Module: ModelMirror::MirrorHelper
- Defined in:
- app/helpers/model_mirror/mirror_helper.rb
Instance Method Summary collapse
- #belongs_to_idx ⇒ Object
- #li_suffix(row, relation) ⇒ Object
- #link_to_mirror(label, model) ⇒ Object
- #link_to_mirror_belongs_to(label, id:, foreign_class:) ⇒ Object
- #link_to_mirror_has_many(label, model:, has_many:) ⇒ Object
- #link_to_mirror_relation(label, model:, relation:) ⇒ Object
- #td_belongs_to?(model_class, key) ⇒ Boolean
- #td_model(row) ⇒ Object
- #th_attributes(attribs) ⇒ Object
Instance Method Details
#belongs_to_idx ⇒ Object
58 59 60 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 58 def belongs_to_idx @__td_belongs_to_idx ||= {} end |
#li_suffix(row, relation) ⇒ Object
62 63 64 65 66 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 62 def li_suffix(row, relation) child = row.send(relation.name) count = child.try(:count) count && " (#{count})" end |
#link_to_mirror(label, model) ⇒ Object
3 4 5 6 7 8 9 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 3 def link_to_mirror(label, model) if Class === model link_to label, mirror_list_url(model_path: model.to_s.underscore) else link_to label, mirror_show_url(model_path: model.class.to_s.underscore, id: model.id) end end |
#link_to_mirror_belongs_to(label, id:, foreign_class:) ⇒ Object
11 12 13 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 11 def link_to_mirror_belongs_to(label, id:, foreign_class:) link_to label, mirror_show_url(model_path: foreign_class.to_s.underscore, id: id) end |
#link_to_mirror_has_many(label, model:, has_many:) ⇒ Object
15 16 17 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 15 def link_to_mirror_has_many(label, model:, has_many:) link_to label, mirror_has_many_url(model_path: model.class.to_s.underscore, id: model.id, has_many: has_many) end |
#link_to_mirror_relation(label, model:, relation:) ⇒ Object
19 20 21 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 19 def link_to_mirror_relation(label, model:, relation:) link_to label, mirror_relation_url(model_path: model.class.to_s.underscore, id: model.id, relation: relation) end |
#td_belongs_to?(model_class, key) ⇒ Boolean
53 54 55 56 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 53 def td_belongs_to?(model_class, key) @__td_belongs_to_idx ||= {} :nope != (@__td_belongs_to_idx[[model_class, key]] ||= (model_class.reflections.detect { |k,r| r.foreign_key == key }.try(:last) || :nope)) end |
#td_model(row) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 31 def td_model(row) row_class = row.class row.as_json.map do |key, value| content_tag(:td) do if model_class.primary_key == key link_to_mirror(value, row) elsif td_belongs_to?(row_class, key) and value relation = belongs_to_idx[[row_class, key]] if relation.polymorphic? poly_class = row.send(relation.foreign_type) poly_id = row.send(relation.foreign_key) link_to value, mirror_show_url(model_path: poly_class.underscore, id: poly_id) else link_to value, mirror_show_url(model_path: relation.klass.to_s.underscore, id: value) end else html_escape value end end end.join.html_safe end |
#th_attributes(attribs) ⇒ Object
23 24 25 26 27 28 29 |
# File 'app/helpers/model_mirror/mirror_helper.rb', line 23 def th_attributes(attribs) attribs.map do |key, _value| content_tag(:th) do html_escape key.try!(:titleize) end end.join.html_safe end |