Module: ActiveAdmin::Decorator::Association
- Defined in:
- lib/activeadmin/decorator/association.rb
Class Method Summary collapse
- .decorate(association, with: nil, parent: nil) ⇒ Object
- .decorate_many(association, with, parent) ⇒ Object
- .decorate_one(element, with, parent) ⇒ Object
- .decorator_class_name_for(parent, klass) ⇒ Object
Class Method Details
.decorate(association, with: nil, parent: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/activeadmin/decorator/association.rb', line 7 def decorate(association, with: nil, parent: nil) raise ArgumentError, "parent or with required" if parent.nil? && with.nil? if association.nil? nil elsif association.respond_to?(:each) decorate_many(association, with, parent) else decorate_one(association, with, parent) end end |
.decorate_many(association, with, parent) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/activeadmin/decorator/association.rb', line 19 def decorate_many(association, with, parent) with ||= if association.is_a?(ActiveRecord::Relation) decorator_class_name_for(parent, association.klass) else decorator_class_name_for(parent, association.first.class) end with = with.constantize if with.is_a?(String) association.map { |item| with.new(item) } end |
.decorate_one(element, with, parent) ⇒ Object
30 31 32 33 34 |
# File 'lib/activeadmin/decorator/association.rb', line 30 def decorate_one(element, with, parent) with ||= decorator_class_name_for(parent, element.class) with = with.constantize if with.is_a?(String) with.new(element) end |
.decorator_class_name_for(parent, klass) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/activeadmin/decorator/association.rb', line 36 def decorator_class_name_for(parent, klass) parent_class_elements = parent.class.name.split("::") prefix = parent_class_elements[0...-1] suffix = parent_class_elements[-1].sub(/^#{parent.model.class.name}/, "") [*prefix, klass].join("::").concat(suffix) end |