added friendly_id

This commit is contained in:
Adrian Hinz 2022-03-22 16:41:15 +01:00
parent 7ad410514f
commit b1ab9dfb9d
11 changed files with 252 additions and 5 deletions

View File

@ -28,6 +28,8 @@ gem 'jbuilder', '~> 2.5'
# gem 'bcrypt', '~> 3.1.7' # gem 'bcrypt', '~> 3.1.7'
gem 'kaminari' gem 'kaminari'
gem 'bootstrap4-kaminari-views' gem 'bootstrap4-kaminari-views'
gem 'wicked_pdf'
gem 'friendly_id', '~> 5.4.0'
# Use ActiveStorage variant # Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8' # gem 'mini_magick', '~> 4.8'

View File

@ -87,6 +87,8 @@ GEM
erubi (1.10.0) erubi (1.10.0)
execjs (2.8.1) execjs (2.8.1)
ffi (1.15.5) ffi (1.15.5)
friendly_id (5.4.2)
activerecord (>= 4.0.0)
globalid (1.0.0) globalid (1.0.0)
activesupport (>= 5.0) activesupport (>= 5.0)
i18n (1.9.1) i18n (1.9.1)
@ -216,6 +218,8 @@ GEM
websocket-driver (0.7.5) websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0) websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5) websocket-extensions (0.1.5)
wicked_pdf (2.1.0)
activesupport
xpath (3.2.0) xpath (3.2.0)
nokogiri (~> 1.8) nokogiri (~> 1.8)
@ -230,6 +234,7 @@ DEPENDENCIES
chromedriver-helper chromedriver-helper
coffee-rails (~> 4.2) coffee-rails (~> 4.2)
devise devise
friendly_id (~> 5.4.0)
jbuilder (~> 2.5) jbuilder (~> 2.5)
kaminari kaminari
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
@ -244,6 +249,7 @@ DEPENDENCIES
tzinfo-data tzinfo-data
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
web-console (>= 3.3.0) web-console (>= 3.3.0)
wicked_pdf
RUBY VERSION RUBY VERSION
ruby 2.6.0p0 ruby 2.6.0p0

View File

@ -23,7 +23,7 @@ class HomeController < ApplicationController
def contact; end def contact; end
def show def show
@dotation = Dotation.find(params[:id]) @dotation = Dotation.friendly.find(params[:id])
@company_sizes = CompanySize.all @company_sizes = CompanySize.all
end end
@ -37,6 +37,10 @@ class HomeController < ApplicationController
SendNotifyEmailJob.perform_later(@emailfilter.id, 1) SendNotifyEmailJob.perform_later(@emailfilter.id, 1)
end end
def prepare_pdf
end
private private
def prepare_filters def prepare_filters

View File

@ -17,7 +17,8 @@ class Dotation < ApplicationRecord
# == Attributes =========================================================== # == Attributes ===========================================================
# == Extensions =========================================================== # == Extensions ===========================================================
extend FriendlyId
friendly_id :name, use: :slugged
# == Relationships ======================================================== # == Relationships ========================================================
belongs_to :expert, optional: true belongs_to :expert, optional: true
belongs_to :partner belongs_to :partner
@ -93,6 +94,6 @@ class Dotation < ApplicationRecord
# == Instance Methods ===================================================== # == Instance Methods =====================================================
def safe_id def safe_id
id friendly_id
end end
end end

View File

@ -0,0 +1,64 @@
# Creates PDF for order
class GeneratePdf
def initialize(order)
@order = order
end
def call
order_to_pdf
end
def path_to_file
"#{Rails.root}/storage/pdfs/zamowienie_#{order.beauty_id}.pdf"
end
def file_name
"zamowienie_#{order.beauty_id}.pdf"
end
private
attr_reader :order
def order_to_html_string
ret = "<html><head><meta charset='utf-8' /><style>body{font-family: "\
'Verdana, Geneva, sans-serif;}tr.border_bottom td {'\
'border-bottom:1pt solid black;}</style></head><body><h2>Zamówienie: '\
"#{order.beauty_id}</h2><br />"\
'<table width="100%"><thead>'\
'<tr><th>Produkt</th><th>'\
'Cena jednostkowa</th><th>Ilość</th><th>Suma</th></tr></thead><tbody>'
order.order_products.each do |op|
ret += "<tr class='border_bottom'><td><strong>#{op.product.name}</strong"\
"></td><td style='text-align:center;'>PLN #{format('%.2f', op.price)}<"\
"/td><td style='text-align:center;'>#{op.quantity}</td><td style='text"\
"-align:right;'>PLN #{format('%.2f', op.multiple_price)}</td></tr>"
end
ret += '</tbody><tfoot>'\
'<tr style="text-align:right;"><td colspan="3">Towar</td>'\
"<td>PLN #{format('%.2f', @order.order_value)}</td></tr>"\
'<tr style="text-align:right;"><td colspan="3">Wysyłka</td>'\
"<td >PLN #{format('%.2f', @order.shipping.price)}</td></tr>"\
'<tr style="text-align:right; font-weight:bold;">'\
'<td colspan="3">Razem do zapłaty</td>'\
"<td>PLN #{format('%.2f', @order.order_summary)}</td></tr></tfoot>"\
"</table><br/><h3>Adres dostawy</h3> <br/>#{order.full_name}<br/>"\
"#{order.address.gsub(/(\r\n|\n\r|\r|\n)/, '<br \>')}"\
'<br/><h3>Wiadomość od klienta:</h3>'\
"#{order.message_from_client.gsub(/(\r\n|\n\r|\r|\n)/, '<br \>')}"\
'</body></html>'
ret
end
def order_to_pdf
pdf = WickedPdf.new.pdf_from_string(order_to_html_string)
save_path = "#{Rails.root}/storage/pdfs"
FileUtils.mkdir_p save_path
file_name = "zamowienie_#{order.beauty_id}.pdf"
save_path += "/#{file_name}"
File.open(save_path, 'wb') do |file|
file << pdf
end
pdf
end
end

View File

@ -67,7 +67,7 @@
</div> </div>
<div class="card-footer"> <div class="card-footer">
<%= link_to 'Zobacz więcej', "/dotacja/#{dotation.id}", class: 'btn-lg btn-success float-right' %> <%= link_to 'Zobacz więcej', "/dotacja/#{dotation.safe_id}", class: 'btn-lg btn-success float-right' %>
</div> </div>
</div> </div>

View File

@ -0,0 +1,107 @@
# FriendlyId Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `friendly_id` class method or defining
# methods in your model.
#
# To learn more, check out the guide:
#
# http://norman.github.io/friendly_id/file.Guide.html
FriendlyId.defaults do |config|
# ## Reserved Words
#
# Some words could conflict with Rails's routes when used as slugs, or are
# undesirable to allow as slugs. Edit this list as needed for your app.
config.use :reserved
config.reserved_words = %w(new edit index session login logout users admin
stylesheets assets javascripts images)
# This adds an option to treat reserved words as conflicts rather than exceptions.
# When there is no good candidate, a UUID will be appended, matching the existing
# conflict behavior.
# config.treat_reserved_as_conflict = true
# ## Friendly Finders
#
# Uncomment this to use friendly finders in all models. By default, if
# you wish to find a record by its friendly id, you must do:
#
# MyModel.friendly.find('foo')
#
# If you uncomment this, you can do:
#
# MyModel.find('foo')
#
# This is significantly more convenient but may not be appropriate for
# all applications, so you must explicity opt-in to this behavior. You can
# always also configure it on a per-model basis if you prefer.
#
# Something else to consider is that using the :finders addon boosts
# performance because it will avoid Rails-internal code that makes runtime
# calls to `Module.extend`.
#
# config.use :finders
#
# ## Slugs
#
# Most applications will use the :slugged module everywhere. If you wish
# to do so, uncomment the following line.
#
# config.use :slugged
#
# By default, FriendlyId's :slugged addon expects the slug column to be named
# 'slug', but you can change it if you wish.
#
# config.slug_column = 'slug'
#
# By default, slug has no size limit, but you can change it if you wish.
#
# config.slug_limit = 255
#
# When FriendlyId can not generate a unique ID from your base method, it appends
# a UUID, separated by a single dash. You can configure the character used as the
# separator. If you're upgrading from FriendlyId 4, you may wish to replace this
# with two dashes.
#
# config.sequence_separator = '-'
#
# Note that you must use the :slugged addon **prior** to the line which
# configures the sequence separator, or else FriendlyId will raise an undefined
# method error.
#
# ## Tips and Tricks
#
# ### Controlling when slugs are generated
#
# As of FriendlyId 5.0, new slugs are generated only when the slug field is
# nil, but if you're using a column as your base method can change this
# behavior by overriding the `should_generate_new_friendly_id?` method that
# FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave
# more like 4.0.
# Note: Use(include) Slugged module in the config if using the anonymous module.
# If you have `friendly_id :name, use: slugged` in the model, Slugged module
# is included after the anonymous module defined in the initializer, so it
# overrides the `should_generate_new_friendly_id?` method from the anonymous module.
#
# config.use :slugged
# config.use Module.new {
# def should_generate_new_friendly_id?
# slug.blank? || <your_column_name_here>_changed?
# end
# }
#
# FriendlyId uses Rails's `parameterize` method to generate slugs, but for
# languages that don't use the Roman alphabet, that's not usually sufficient.
# Here we use the Babosa library to transliterate Russian Cyrillic slugs to
# ASCII. If you use this, don't forget to add "babosa" to your Gemfile.
#
# config.use Module.new {
# def normalize_friendly_id(text)
# text.to_slug.normalize! :transliterations => [:russian, :latin]
# end
# }
end

View File

@ -0,0 +1,22 @@
# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
exe_path: 'c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
# exe_path: '/usr/bin/wkhtmltopdf'
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
}

View File

@ -0,0 +1,6 @@
class AddSlugToDotations < ActiveRecord::Migration[5.2]
def change
add_column :dotations, :slug, :string, after: :id
add_index :dotations, :slug, unique: true
end
end

View File

@ -0,0 +1,21 @@
MIGRATION_CLASS =
if ActiveRecord::VERSION::MAJOR >= 5
ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
else
ActiveRecord::Migration
end
class CreateFriendlyIdSlugs < MIGRATION_CLASS
def change
create_table :friendly_id_slugs do |t|
t.string :slug, :null => false
t.integer :sluggable_id, :null => false
t.string :sluggable_type, :limit => 50
t.string :scope
t.datetime :created_at
end
add_index :friendly_id_slugs, [:sluggable_type, :sluggable_id]
add_index :friendly_id_slugs, [:slug, :sluggable_type], length: { slug: 140, sluggable_type: 50 }
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], length: { slug: 70, sluggable_type: 50, scope: 70 }, unique: true
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_03_11_073315) do ActiveRecord::Schema.define(version: 2022_03_22_143440) do
create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
@ -71,6 +71,7 @@ ActiveRecord::Schema.define(version: 2022_03_11_073315) do
end end
create_table "dotations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "dotations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "slug"
t.string "name" t.string "name"
t.string "formal_name" t.string "formal_name"
t.date "date_from" t.date "date_from"
@ -107,6 +108,7 @@ ActiveRecord::Schema.define(version: 2022_03_11_073315) do
t.index ["formal_name"], name: "index_dotations_on_formal_name" t.index ["formal_name"], name: "index_dotations_on_formal_name"
t.index ["name"], name: "index_dotations_on_name" t.index ["name"], name: "index_dotations_on_name"
t.index ["points"], name: "index_dotations_on_points" t.index ["points"], name: "index_dotations_on_points"
t.index ["slug"], name: "index_dotations_on_slug", unique: true
t.index ["start_date"], name: "index_dotations_on_start_date" t.index ["start_date"], name: "index_dotations_on_start_date"
end end
@ -148,11 +150,23 @@ ActiveRecord::Schema.define(version: 2022_03_11_073315) do
create_table "filter_for_emails", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "filter_for_emails", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "email" t.string "email"
t.string "unique_name"
t.text "filters" t.text "filters"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "friendly_id_slugs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true, length: { slug: 70, scope: 70 }
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", length: { slug: 140 }
t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
end
create_table "partners", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "partners", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name" t.string "name"
t.text "description" t.text "description"