Class: Crm::RenderedProductSpecificationPresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/crm/rendered_product_specification_presenter.rb

Instance Attribute Summary

Attributes inherited from BasePresenter

#current_account, #options, #url_helper

Instance Method Summary collapse

Methods inherited from BasePresenter

#initialize

Methods inherited from BasePresenter

#can?, #capture, #concat, #content_tag, #fa_icon, #h, #initialize, #link_to, #number_to_currency, #present, presents, #r, #safe_present, #simple_format, #u

Constructor Details

This class inherits a constructor from Crm::BasePresenter

Instance Method Details



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 159

def clone_link(item, label: nil)
  return unless product_specification

  clone_params = {}
  # if we're cloning an item specific spec we don't clone the item id
  clone_params[:item_ids] = [item.id] if item && product_specification.items.size > 1
  # reset product line id
  clone_params[:product_line_id] = nil
  clone_params[:product_category_id] = nil
  clone_params[:method] = 'text'
  h.link_to(h.fa_icon('clone', text: label, title: 'Clone spec/Create unique'),
            h.clone_product_specification_path(product_specification_id,
                                               product_specification: clone_params,
                                               return_path:),
            method: :post)
end

#copy_buttonObject



31
32
33
34
35
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 31

def copy_button
  return if output.blank?

  h.clipboard_copy('Copy', mode: :text, copy_value: output, button_class: 'dropdown-item')
end

#dynamic_buttonObject



37
38
39
40
41
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 37

def dynamic_button
  return unless type == 'Dynamic'

  { text: 'Dynamic Value', icon: 'calculator' }
end

#edit_buttonObject



22
23
24
25
26
27
28
29
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 22

def edit_button
  return unless product_specification_id

  label = "#{name}"
  label << " #{fa_icon('calculator')}".html_safe if type == 'Dynamic'
  h.link_to label.html_safe,
            h.edit_product_specification_path(product_specification_id, return_path:)
end

#edit_combo_button(item) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 8

def edit_combo_button(item)
  links = []
  links << edit_button
  links << copy_button
  links << clone_link(item, label: 'Clone')
  links << visibility_output
  links << { content: "Length: #{output&.to_s&.size}" } if output.present?
  spec_origin_array.each do |origin|
    links << { content: origin, class: 'dropdown-item-text' }
  end

  h.render_combo_drop_down links.compact, main_link_class: 'btn btn-link'
end

#groupingObject



99
100
101
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 99

def grouping
  r[1]['grouping']
end

#imageObject



59
60
61
62
63
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 59

def image
  return unless (iid = r[1]['image_id'])

  Image.find(iid)
end

#item_skusObject



125
126
127
128
129
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 125

def item_skus
  return unless origin_object

  origin_object.items.pluck(:sku)
end

#nameObject



43
44
45
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 43

def name
  r[1]['name']
end

#origin_objectObject



109
110
111
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 109

def origin_object
  product_specification
end

#outputObject



51
52
53
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 51

def output
  r[1]['output']
end

#output_with_warning(length_limit: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 65

def output_with_warning(length_limit: nil)
  if raw.present?
    if length_limit
      h.truncate(output.to_s, length: length_limit, omission: '...') { link_to '...(edit)', h.edit_product_specification_path(product_specification_id, return_path:) }
    else
      output.to_s.html_safe
    end
  else
    h.fa_icon('exclamation-triangle', style: 'color: red', text: 'Click to Edit')
  end
end

#product_category_nameObject



119
120
121
122
123
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 119

def product_category_name
  return unless origin_object

  origin_object.product_category.try(:lineage_expanded)&.gsub('Goods > ', '')
end

#product_line_nameObject



113
114
115
116
117
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 113

def product_line_name
  return unless origin_object

  origin_object.product_line.try(:lineage_expanded)
end

#product_specificationObject



103
104
105
106
107
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 103

def product_specification
  r[1]['product_specification'] || ProductSpecification.includes(:items).find(product_specification_id)
rescue StandardError
  nil
end

#product_specification_idObject



95
96
97
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 95

def product_specification_id
  r[1]['product_specification_id']
end

#rawObject



47
48
49
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 47

def raw
  r[1]['raw']
end

#return_pathObject



4
5
6
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 4

def return_path
  @options[:return_path]
end

#sku_regexpObject



55
56
57
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 55

def sku_regexp
  r[1]['sku_regexp']
end

#spec_originObject



149
150
151
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 149

def spec_origin
  spec_origin_array.join('<br>').html_safe
end

#spec_origin_arrayObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 131

def spec_origin_array
  str = []
  if item_skus.present?
    str << item_skus.shift
    str << "and #{item_skus.length} more" if item_skus.length > 1
    if template_product_specification
      str << h.link_to(h.edit_product_specification_path(template_product_specification, return_path:)) do
        h.(:span, 'Template', class: 'badge bg-secondary', title: "#{[template_product_specification.product_line.try(:lineage_expanded), template_product_specification.product_category.try(:lineage_expanded)].compact.join(' / ')}")
      end
    end
  else
    str << product_line_name
    str << product_category_name
    str << product_specification&.sku_regexp
  end
  str.compact
end

#template_product_specificationObject



153
154
155
156
157
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 153

def template_product_specification
  return unless product_specification

  product_specification.template_product_specification
end

#typeObject



87
88
89
90
91
92
93
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 87

def type
  if r[1]['static'].nil?
    'Unknown'
  else
    r[1]['static'].to_b ? 'Static' : 'Dynamic'
  end
end

#visibilityObject



77
78
79
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 77

def visibility
  r[1]['visibility']
end

#visibility_outputObject



81
82
83
84
85
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 81

def visibility_output
  return if visibility == 'open_visibility'

  { content: 'Not Public' }
end