From b1ab9dfb9d47193c3ea330decc02f7c04d40d7bb Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Tue, 22 Mar 2022 16:41:15 +0100 Subject: [PATCH] added friendly_id --- Gemfile | 2 + Gemfile.lock | 6 + app/controllers/home_controller.rb | 6 +- app/models/dotation.rb | 5 +- app/services/generate_pdf.rb | 64 +++++++++++ app/views/home/_dotation_card.html.erb | 2 +- config/initializers/friendly_id.rb | 107 ++++++++++++++++++ config/initializers/wicked_pdf.rb | 22 ++++ .../20220322143427_add_slug_to_dotations.rb | 6 + ...20220322143440_create_friendly_id_slugs.rb | 21 ++++ db/schema.rb | 16 ++- 11 files changed, 252 insertions(+), 5 deletions(-) create mode 100644 app/services/generate_pdf.rb create mode 100644 config/initializers/friendly_id.rb create mode 100644 config/initializers/wicked_pdf.rb create mode 100644 db/migrate/20220322143427_add_slug_to_dotations.rb create mode 100644 db/migrate/20220322143440_create_friendly_id_slugs.rb diff --git a/Gemfile b/Gemfile index 7d54631..2bb10b8 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,8 @@ gem 'jbuilder', '~> 2.5' # gem 'bcrypt', '~> 3.1.7' gem 'kaminari' gem 'bootstrap4-kaminari-views' +gem 'wicked_pdf' +gem 'friendly_id', '~> 5.4.0' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' diff --git a/Gemfile.lock b/Gemfile.lock index d2551bd..309a646 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,6 +87,8 @@ GEM erubi (1.10.0) execjs (2.8.1) ffi (1.15.5) + friendly_id (5.4.2) + activerecord (>= 4.0.0) globalid (1.0.0) activesupport (>= 5.0) i18n (1.9.1) @@ -216,6 +218,8 @@ GEM websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) + wicked_pdf (2.1.0) + activesupport xpath (3.2.0) nokogiri (~> 1.8) @@ -230,6 +234,7 @@ DEPENDENCIES chromedriver-helper coffee-rails (~> 4.2) devise + friendly_id (~> 5.4.0) jbuilder (~> 2.5) kaminari listen (>= 3.0.5, < 3.2) @@ -244,6 +249,7 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console (>= 3.3.0) + wicked_pdf RUBY VERSION ruby 2.6.0p0 diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c90e58c..2a8b55b 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -23,7 +23,7 @@ class HomeController < ApplicationController def contact; end def show - @dotation = Dotation.find(params[:id]) + @dotation = Dotation.friendly.find(params[:id]) @company_sizes = CompanySize.all end @@ -37,6 +37,10 @@ class HomeController < ApplicationController SendNotifyEmailJob.perform_later(@emailfilter.id, 1) end + def prepare_pdf + + end + private def prepare_filters diff --git a/app/models/dotation.rb b/app/models/dotation.rb index 3a5b5d4..aa646b2 100644 --- a/app/models/dotation.rb +++ b/app/models/dotation.rb @@ -17,7 +17,8 @@ class Dotation < ApplicationRecord # == Attributes =========================================================== # == Extensions =========================================================== - + extend FriendlyId + friendly_id :name, use: :slugged # == Relationships ======================================================== belongs_to :expert, optional: true belongs_to :partner @@ -93,6 +94,6 @@ class Dotation < ApplicationRecord # == Instance Methods ===================================================== def safe_id - id + friendly_id end end diff --git a/app/services/generate_pdf.rb b/app/services/generate_pdf.rb new file mode 100644 index 0000000..cafa238 --- /dev/null +++ b/app/services/generate_pdf.rb @@ -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 = "

Zamówienie: '\ + "#{order.beauty_id}


"\ + ''\ + '' + order.order_products.each do |op| + ret += "" + end + ret += ''\ + ''\ + ""\ + ''\ + ""\ + ''\ + ''\ + ""\ + "
Produkt'\ + 'Cena jednostkowaIlośćSuma
#{op.product.name}PLN #{format('%.2f', op.price)}<"\ + "/td>#{op.quantity}PLN #{format('%.2f', op.multiple_price)}
TowarPLN #{format('%.2f', @order.order_value)}
WysyłkaPLN #{format('%.2f', @order.shipping.price)}
Razem do zapłatyPLN #{format('%.2f', @order.order_summary)}

Adres dostawy


#{order.full_name}
"\ + "#{order.address.gsub(/(\r\n|\n\r|\r|\n)/, '
')}"\ + '

Wiadomość od klienta:

'\ + "#{order.message_from_client.gsub(/(\r\n|\n\r|\r|\n)/, '
')}"\ + '' + 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 diff --git a/app/views/home/_dotation_card.html.erb b/app/views/home/_dotation_card.html.erb index bb0c280..8e2dde5 100644 --- a/app/views/home/_dotation_card.html.erb +++ b/app/views/home/_dotation_card.html.erb @@ -67,7 +67,7 @@ diff --git a/config/initializers/friendly_id.rb b/config/initializers/friendly_id.rb new file mode 100644 index 0000000..d557afe --- /dev/null +++ b/config/initializers/friendly_id.rb @@ -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? || _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 diff --git a/config/initializers/wicked_pdf.rb b/config/initializers/wicked_pdf.rb new file mode 100644 index 0000000..beb8191 --- /dev/null +++ b/config/initializers/wicked_pdf.rb @@ -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', +} diff --git a/db/migrate/20220322143427_add_slug_to_dotations.rb b/db/migrate/20220322143427_add_slug_to_dotations.rb new file mode 100644 index 0000000..06de10f --- /dev/null +++ b/db/migrate/20220322143427_add_slug_to_dotations.rb @@ -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 diff --git a/db/migrate/20220322143440_create_friendly_id_slugs.rb b/db/migrate/20220322143440_create_friendly_id_slugs.rb new file mode 100644 index 0000000..aefc991 --- /dev/null +++ b/db/migrate/20220322143440_create_friendly_id_slugs.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index c68a60c..9a18f59 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.string "name", null: false @@ -71,6 +71,7 @@ ActiveRecord::Schema.define(version: 2022_03_11_073315) do end create_table "dotations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| + t.string "slug" t.string "name" t.string "formal_name" 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 ["name"], name: "index_dotations_on_name" 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" 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| t.string "email" + t.string "unique_name" t.text "filters" t.datetime "created_at", null: false t.datetime "updated_at", null: false 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| t.string "name" t.text "description"