Class: Olivander::Components::ResourceFormComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Olivander::Components::ResourceFormComponent
- Defined in:
- app/components/olivander/components/resource_form_component.rb
Instance Method Summary collapse
- #association?(field) ⇒ Boolean
- #association_data_hash_for(field) ⇒ Object
- #boolean?(field) ⇒ Boolean
- #collection_for(field) ⇒ Object
- #collection_path_for(field) ⇒ Object
-
#initialize(resource, form_builder) ⇒ ResourceFormComponent
constructor
A new instance of ResourceFormComponent.
- #input_data_hash_for(field) ⇒ Object
- #input_picker_options_for(field) ⇒ Object
- #label_data_hash_for(field) ⇒ Object
- #one_through?(field) ⇒ Boolean
- #tag_field_name(field) ⇒ Object
- #taggable?(field) ⇒ Boolean
Constructor Details
#initialize(resource, form_builder) ⇒ ResourceFormComponent
Returns a new instance of ResourceFormComponent.
7 8 9 10 11 |
# File 'app/components/olivander/components/resource_form_component.rb', line 7 def initialize(resource, form_builder) @resource = resource @f = form_builder super end |
Instance Method Details
#association?(field) ⇒ Boolean
71 72 73 74 75 |
# File 'app/components/olivander/components/resource_form_component.rb', line 71 def association?(field) %i[ association belongs_to_association has_many_association has_many_through_association has_and_belongs_to_many_reflection has_one_through_association ].include?(field.type) end |
#association_data_hash_for(field) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'app/components/olivander/components/resource_form_component.rb', line 24 def association_data_hash_for(field) { collection_path: collection_path_for(field), controller: "association-#{@resource.class.name.underscore.dasherize.gsub('/', '-')}-#{field.sym} input-control-association", taggable: taggable?(field), tag_field_name: tag_field_name(field) }.merge(label_data_hash_for(field)) end |
#boolean?(field) ⇒ Boolean
81 82 83 |
# File 'app/components/olivander/components/resource_form_component.rb', line 81 def boolean?(field) field.type == :boolean end |
#collection_for(field) ⇒ Object
13 14 15 |
# File 'app/components/olivander/components/resource_form_component.rb', line 13 def collection_for(field) @f.object.send(field.sym) end |
#collection_path_for(field) ⇒ Object
65 66 67 68 69 |
# File 'app/components/olivander/components/resource_form_component.rb', line 65 def collection_path_for(field) polymorphic_path(@resource.class.reflect_on_association(field.sym).klass, format: :json) rescue StandardError '' end |
#input_data_hash_for(field) ⇒ Object
34 35 36 37 38 39 |
# File 'app/components/olivander/components/resource_form_component.rb', line 34 def input_data_hash_for(field) { controller: "input-#{@resource.class.name.underscore.dasherize.gsub('/', '-')}-#{field.sym} input-control-#{field.type}" }.merge(label_data_hash_for(field)) end |
#input_picker_options_for(field) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'app/components/olivander/components/resource_form_component.rb', line 41 def (field) case field.type when :datetime { icons: { time: 'far fa-clock' } } when :date { 'format': 'MM/DD/YYYY' } else {} end end |
#label_data_hash_for(field) ⇒ Object
17 18 19 20 21 22 |
# File 'app/components/olivander/components/resource_form_component.rb', line 17 def label_data_hash_for(field) help_text_key = ['activerecord.attributes', @resource.class.name.underscore, "#{field.sym}_help_text"].join('.') {}.tap do |hash| hash[:popper_text] = O18n.t(help_text_key) if I18n.exists?(help_text_key) end end |
#one_through?(field) ⇒ Boolean
77 78 79 |
# File 'app/components/olivander/components/resource_form_component.rb', line 77 def one_through?(field) field.type == :has_one_through_association end |
#tag_field_name(field) ⇒ Object
59 60 61 62 63 |
# File 'app/components/olivander/components/resource_form_component.rb', line 59 def tag_field_name(field) return '' unless taggable?(field) @resource.class.send("#{field.sym}_tag_field_name") end |
#taggable?(field) ⇒ Boolean
52 53 54 55 56 57 |
# File 'app/components/olivander/components/resource_form_component.rb', line 52 def taggable?(field) method_key = "#{field.sym}_taggable?" return false unless @resource.class.respond_to?(method_key) @resource.class.send(method_key) end |