Class: MenuSectionsReportPdf
- Inherits:
-
Prawn::Document
- Object
- Prawn::Document
- MenuSectionsReportPdf
- Includes:
- MoneyRails::ActionViewExtension
- Defined in:
- app/my_lib/menu_sections_report_pdf.rb
Constant Summary collapse
- TABLE_COLUMN_WIDTHS =
[ 80, 80, 80, 80, 80, 80, 80, 80, 80 ]
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#date_filter_type ⇒ Object
Returns the value of attribute date_filter_type.
-
#date_from ⇒ Object
Returns the value of attribute date_from.
-
#date_to ⇒ Object
Returns the value of attribute date_to.
-
#emails ⇒ Object
Returns the value of attribute emails.
-
#menu_items_ids ⇒ Object
Returns the value of attribute menu_items_ids.
-
#menu_items_type ⇒ Object
Returns the value of attribute menu_items_type.
-
#menu_sections ⇒ Object
Returns the value of attribute menu_sections.
-
#pdf_url ⇒ Object
Returns the value of attribute pdf_url.
-
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #download_link ⇒ Object
- #filepath ⇒ Object
- #generate_pdf(menu_sections, args = {}) ⇒ Object
- #generate_pdf_table ⇒ Object
-
#initialize(menu_sections = nil, args = {}) ⇒ MenuSectionsReportPdf
constructor
A new instance of MenuSectionsReportPdf.
- #save_attachment ⇒ Object
- #store_to_attachment ⇒ Object
Constructor Details
#initialize(menu_sections = nil, args = {}) ⇒ MenuSectionsReportPdf
Returns a new instance of MenuSectionsReportPdf.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 25 def initialize(=nil, args={}) @menu_sections = if args.present? args.to_h.with_indifferent_access.each do |k, v| send("#{k}=".to_sym, v) end end super(top_margin: 10, page_size: "A4", page_layout: :landscape) self.font_families.update("Mitr" => { :normal => Rails.root.join("vendor/assets/fonts/mitr/Mitr-Regular.ttf"), :bold => Rails.root.join("vendor/assets/fonts/mitr/Mitr-Bold.ttf") }) font "Mitr" @pdf_url = {} end |
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def date @date end |
#date_filter_type ⇒ Object
Returns the value of attribute date_filter_type.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def date_filter_type @date_filter_type end |
#date_from ⇒ Object
Returns the value of attribute date_from.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def date_from @date_from end |
#date_to ⇒ Object
Returns the value of attribute date_to.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def date_to @date_to end |
#emails ⇒ Object
Returns the value of attribute emails.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def emails @emails end |
#menu_items_ids ⇒ Object
Returns the value of attribute menu_items_ids.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def @menu_items_ids end |
#menu_items_type ⇒ Object
Returns the value of attribute menu_items_type.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def @menu_items_type end |
#menu_sections ⇒ Object
Returns the value of attribute menu_sections.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def @menu_sections end |
#pdf_url ⇒ Object
Returns the value of attribute pdf_url.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def pdf_url @pdf_url end |
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def restaurant_id @restaurant_id end |
#type ⇒ Object
Returns the value of attribute type.
19 20 21 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19 def type @type end |
Instance Method Details
#download_link ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 115 def download_link link = .pdf_url link = "#{Figaro.env.HH_HOST_URL!}#{link}" unless link.include? 'http' return link unless Figaro.bool_env! :ENABLE_CDN link.gsub(Figaro.env.CDN_URL, Figaro.env.HH_HOST_URL) link end |
#filepath ⇒ Object
111 112 113 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 111 def filepath Rails.root.join('tmp', 'reports', file_name).to_s end |
#generate_pdf(menu_sections, args = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 85 def generate_pdf(, args={}) args[:file_name] = "Hungry-Hub-report-menu-items#{Time.zone.today}-#{restaurant_id}#{Time.current_time.to_i}.pdf" file_pdf = self.class.new(, args) file_pdf.generate_pdf_table file_pdf.render_file(file_pdf.filepath) path = Rails.root.join(file_pdf.filepath) loop do break if path.exist? sleep 1 end file_pdf. @pdf_url = file_pdf.download_link end |
#generate_pdf_table ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 43 def generate_pdf_table I18n.locale = :en data = [] image_url = AdminSetting.report_pdf_image_url = [[{:image => open(image_url), image_height: 110, image_width: 530, position: :center}]] if image_url.present? label = [] label += [['Restaurant Name', restaurant.name]] bounding_box([0, 510], width: 230) do table(label, cell_style: {border_width: 0}) # transparent(0.5) { stroke_bounds } end if .present? bounding_box([370, 530], width: 250) do table(, cell_style: {border_width: 0}) # transparent(0.5) { stroke_bounds } end end move_down 20 data.push(%w[Restaurant-ID Restaurant-Name Name-en Name-th Package-In-Use Menu-Items-count Created Updated Menu-Items ]) .find_each do |v| row_data = [ restaurant.id, restaurant.name, v.name_en, v.name_th, v.package ? 1 : 0, v. ? v..size : 0, v&.created_at&.strftime('%d %b %Y'), v&.updated_at&.strftime('%d %b %Y'), v..joins(:translations).group(:id).pluck(:name).map(&:inspect).join(', ') ] data.push(row_data) end table(data, :column_widths => TABLE_COLUMN_WIDTHS, :header => true) end |
#save_attachment ⇒ Object
101 102 103 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 101 def self. = end |
#store_to_attachment ⇒ Object
105 106 107 108 109 |
# File 'app/my_lib/menu_sections_report_pdf.rb', line 105 def = Attachment.new pdf: open(filepath) .save! end |