Class: Crm::RenderedProductSpecificationPresenter

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

Overview

Presenter: rendered product specification presenter.

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



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

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



33
34
35
36
37
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 33

def copy_button
  return if output.blank?

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

#dynamic_buttonObject



39
40
41
42
43
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 39

def dynamic_button
  return unless type == 'Dynamic'

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

#edit_buttonObject



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

def edit_button
  return unless product_specification_id

  label = name.to_s
  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



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

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



101
102
103
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 101

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

#imageObject



61
62
63
64
65
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 61

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

  Image.find(iid)
end

#item_skusObject



127
128
129
130
131
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 127

def item_skus
  return unless origin_object

  origin_object.items.pluck(:sku)
end

#nameObject



45
46
47
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 45

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

#origin_objectObject



111
112
113
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 111

def origin_object
  product_specification
end

#outputObject



53
54
55
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 53

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

#output_with_warning(length_limit: nil) ⇒ Object



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

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



121
122
123
124
125
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 121

def product_category_name
  return unless origin_object

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

#product_line_nameObject



115
116
117
118
119
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 115

def product_line_name
  return unless origin_object

  origin_object.product_line.try(:lineage_expanded)
end

#product_specificationObject



105
106
107
108
109
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 105

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

#product_specification_idObject



97
98
99
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 97

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

#rawObject



49
50
51
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 49

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

#return_pathObject



6
7
8
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 6

def return_path
  @options[:return_path]
end

#sku_regexpObject



57
58
59
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 57

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

#spec_originObject



151
152
153
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 151

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

#spec_origin_arrayObject



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

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(' / ').to_s)
      end
    end
  else
    str << product_line_name
    str << product_category_name
    str << product_specification&.sku_regexp
  end
  str.compact
end

#template_product_specificationObject



155
156
157
158
159
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 155

def template_product_specification
  return unless product_specification

  product_specification.template_product_specification
end

#typeObject



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

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

#visibilityObject



79
80
81
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 79

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

#visibility_outputObject



83
84
85
86
87
# File 'app/presenters/crm/rendered_product_specification_presenter.rb', line 83

def visibility_output
  return if visibility == 'open_visibility'

  { content: 'Not Public' }
end