Module: SimpleDecorator::CommonDecorated::ClassMethods
- Defined in:
- lib/simple_decorator/common_decorated.rb
Instance Method Summary collapse
- #associations ⇒ Object
- #columns(type = nil, name: nil) ⇒ Object
- #decorate(*attributes, &block) ⇒ Object
- #enumerations ⇒ Object
- #humanize(*attributes) ⇒ Object
- #inherited(child) ⇒ Object
- #localize(*attributes) ⇒ Object
- #translate(*attributes) ⇒ Object
- #with_precision(*attributes) ⇒ Object
Instance Method Details
#associations ⇒ Object
27 28 29 |
# File 'lib/simple_decorator/common_decorated.rb', line 27 def associations columns(name: /_id$/).map { |name| name.sub(/_id$/, '') } end |
#columns(type = nil, name: nil) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/simple_decorator/common_decorated.rb', line 31 def columns(type = nil, name: nil) columns = Array(decorated.try(:columns)) columns = columns.select { |column| column.type == type } if type columns = columns.select { |column| column.name =~ name } if name columns.map(&:name) end |
#decorate(*attributes, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/simple_decorator/common_decorated.rb', line 63 def decorate(*attributes, &block) = attributes. prefix = .delete(:prefix) sufix = .delete(:sufix) attributes.each do |attr| define_method "#{prefix}#{attr}#{sufix}" do value = component.send attr block_given? ? instance_exec(value, , attr, &block) : value.to_s end end end |
#enumerations ⇒ Object
23 24 25 |
# File 'lib/simple_decorator/common_decorated.rb', line 23 def enumerations Hash(decorated.try(:enumerations)).keys end |
#humanize(*attributes) ⇒ Object
57 58 59 60 61 |
# File 'lib/simple_decorator/common_decorated.rb', line 57 def humanize(*attributes) decorate *attributes do |_value, , attr| component.send "#{attr}_humanize" end end |
#inherited(child) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/simple_decorator/common_decorated.rb', line 10 def inherited(child) self.decorated = child.name.sub(/Decorator$/, '').safe_constantize child.with_precision *columns(:decimal) child.translate *columns(:boolean) child.localize *columns(:date) child.localize *columns(:date), format: :long, sufix: '_in_words' child.localize *columns(:datetime), format: :medium child.localize *columns(:datetime), sufix: '_in_words' child.humanize *enumerations child.decorate *associations end |
#localize(*attributes) ⇒ Object
51 52 53 54 55 |
# File 'lib/simple_decorator/common_decorated.rb', line 51 def localize(*attributes) decorate *attributes do |value, | localize value, if value.present? end end |
#translate(*attributes) ⇒ Object
45 46 47 48 49 |
# File 'lib/simple_decorator/common_decorated.rb', line 45 def translate(*attributes) decorate *attributes do |value, | translate value.to_s, if value.to_s.present? end end |
#with_precision(*attributes) ⇒ Object
39 40 41 42 43 |
# File 'lib/simple_decorator/common_decorated.rb', line 39 def with_precision(*attributes) decorate *attributes do |value, | number_with_precision (value || 0), end end |