Module: Templaty::Helper
- Defined in:
- lib/templaty/helper.rb
Class Method Summary collapse
- .belongs_to(options) ⇒ Object
- .data(field) ⇒ Object
- .data_as_hash_string(data, value_attribute: :formatted) ⇒ Object
- .data_for(options) ⇒ Object
- .data_i18n(options) ⇒ Object
- .data_wrap(value) ⇒ Object
- .fields(options) ⇒ Object
- .fields_grid(options) ⇒ Object
- .fields_grid_style(options) ⇒ Object
- .fields_i18n(options) ⇒ Object
- .fields_presence(options) ⇒ Object
- .i18n_created(options) ⇒ Object
- .i18n_new(options) ⇒ Object
- .i18n_removed(options) ⇒ Object
- .i18n_this(options) ⇒ Object
- .i18n_updated(options) ⇒ Object
- .rspec_matcher(value) ⇒ Object
- .validates_numericality(options) ⇒ Object
Class Method Details
.belongs_to(options) ⇒ Object
9 10 11 |
# File 'lib/templaty/helper.rb', line 9 def belongs_to() [:belongs_to].split(',') end |
.data(field) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/templaty/helper.rb', line 13 def data(field) amount = Faker::Number.number(digits: 5) description = Faker::Lorem.paragraphs.join("\r\n\n") name = Faker::Artist.name percentage = Faker::Number.number(digits: 4) { 'amount_cents' => { raw: amount, formatted: ::Helper.money(amount, type: :cents) }, 'description' => { raw: description, formatted: description }, 'name' => { raw: name, formatted: name }, 'percentage_cents' => { raw: percentage, formatted: ::Helper.percentage(percentage, type: :cents) }, }[field] || { field => { raw: 'any', formatted: 'any' } } end |
.data_as_hash_string(data, value_attribute: :formatted) ⇒ Object
27 28 29 |
# File 'lib/templaty/helper.rb', line 27 def data_as_hash_string(data, value_attribute: :formatted) data.map { |key, value| "#{key}: #{data_wrap(value[value_attribute])}" }.join(', ') end |
.data_for(options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/templaty/helper.rb', line 31 def data_for() fields() .sort .reduce({}) { |acc, field| acc.tap { |data| data[field] = Templaty::Helper.data(field) } } .tap do |_field, item| if [:avatar] item << { 'avatar' => { raw: "fixture_image('image.jpg')", formatted: "fixture_image('image.jpg')" } } end if [:cover] item << { 'cover' => { raw: "fixture_image('image.png')", formatted: "fixture_image('image.png')" } } end if [:photos] item << { 'photos' => { raw: "[fixture_image('image-1.jpg'), fixture_image('image-2.jpg')]", formatted: "[fixture_image('image-1.jpg'), fixture_image('image-2.jpg')]", }, } end end end |
.data_i18n(options) ⇒ Object
55 56 57 |
# File 'lib/templaty/helper.rb', line 55 def data_i18n() fields().zip(fields_i18n()).to_h end |
.data_wrap(value) ⇒ Object
59 60 61 |
# File 'lib/templaty/helper.rb', line 59 def data_wrap(value) value.is_a?(String) ? "'#{value}'" : value end |
.fields(options) ⇒ Object
63 64 65 |
# File 'lib/templaty/helper.rb', line 63 def fields() [:fields].split(',') end |
.fields_grid(options) ⇒ Object
75 76 77 |
# File 'lib/templaty/helper.rb', line 75 def fields_grid() [:fields_grid].split(',').map { |item| [item.split(':')].flatten.map(&:to_i) } end |
.fields_grid_style(options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/templaty/helper.rb', line 79 def fields_grid_style() fields_grid().flatten.map do |number| case number.to_i when 50 { 'margin-left' => '16px', 'width' => 'calc(50% - 24px)', } when 100 { 'margin-left' => '16px', 'width' => 'calc(100% - 32px)', } end end end |
.fields_i18n(options) ⇒ Object
67 68 69 |
# File 'lib/templaty/helper.rb', line 67 def fields_i18n() [:fields_i18n].split(',') end |
.fields_presence(options) ⇒ Object
71 72 73 |
# File 'lib/templaty/helper.rb', line 71 def fields_presence() [:fields_presence].split(',') end |
.i18n_created(options) ⇒ Object
96 97 98 |
# File 'lib/templaty/helper.rb', line 96 def i18n_created() [:gender] == 'male' ? 'criado' : 'criada' end |
.i18n_new(options) ⇒ Object
100 101 102 |
# File 'lib/templaty/helper.rb', line 100 def i18n_new() [:gender] == 'male' ? 'novo' : 'nova' end |
.i18n_removed(options) ⇒ Object
104 105 106 |
# File 'lib/templaty/helper.rb', line 104 def i18n_removed() [:gender] == 'male' ? 'removido' : 'removida' end |
.i18n_this(options) ⇒ Object
108 109 110 |
# File 'lib/templaty/helper.rb', line 108 def i18n_this() [:gender] == 'male' ? 'este' : 'esta' end |
.i18n_updated(options) ⇒ Object
112 113 114 |
# File 'lib/templaty/helper.rb', line 112 def i18n_updated() [:gender] == 'male' ? 'atualizado' : 'atualizada' end |
.rspec_matcher(value) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/templaty/helper.rb', line 116 def rspec_matcher(value) return "be(#{value.inspect})" if value.nil? || value.is_a?(Numeric) return "eq('')" if value == '' "eq('#{value}')" end |
.validates_numericality(options) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/templaty/helper.rb', line 123 def validates_numericality() [:validates_numericality]&.split(',')&.reduce({}) do |acc, item| field, min, max = item.split(':') acc.tap { |data| data[field] = { min: min, max: max } } end end |