Class: Olivander::Components::ResourceFormComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/olivander/components/resource_form_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource, form_builder) ⇒ ResourceFormComponent

Returns a new instance of ResourceFormComponent.



6
7
8
9
10
# File 'app/components/olivander/components/resource_form_component.rb', line 6

def initialize(resource, form_builder)
  @resource = resource
  @f = form_builder
  super
end

Instance Method Details

#association?(field) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'app/components/olivander/components/resource_form_component.rb', line 52

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



16
17
18
19
20
21
22
23
# File 'app/components/olivander/components/resource_form_component.rb', line 16

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)
  }
end

#boolean?(field) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/components/olivander/components/resource_form_component.rb', line 62

def boolean?(field)
  field.type == :boolean
end

#collection_for(field) ⇒ Object



12
13
14
# File 'app/components/olivander/components/resource_form_component.rb', line 12

def collection_for(field)
  @f.object.send(field.sym)
end

#collection_path_for(field) ⇒ Object



44
45
46
47
48
49
50
# File 'app/components/olivander/components/resource_form_component.rb', line 44

def collection_path_for(field)
  begin
    polymorphic_path(@resource.class.reflect_on_association(field.sym).klass, format: :json)
  rescue
    ''
  end
end

#input_data_hash_for(field) ⇒ Object



25
26
27
28
29
# File 'app/components/olivander/components/resource_form_component.rb', line 25

def input_data_hash_for(field)
  {
    controller: "input-#{@resource.class.name.underscore.dasherize.gsub('/', '-')}-#{field.sym} input-control-#{field.type}",
  }
end

#one_through?(field) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/components/olivander/components/resource_form_component.rb', line 58

def one_through?(field)
  field.type == :has_one_through_association
end

#tag_field_name(field) ⇒ Object



38
39
40
41
42
# File 'app/components/olivander/components/resource_form_component.rb', line 38

def tag_field_name(field)
  return '' unless taggable?(field)

  @resource.class.send("#{field.sym}_tag_field_name")
end

#taggable?(field) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'app/components/olivander/components/resource_form_component.rb', line 31

def taggable?(field)
  method_key = "#{field.sym}_taggable?"
  return false unless @resource.class.respond_to?(method_key)

  @resource.class.send(method_key)
end