Class: Infold::ControllerWriter

Inherits:
BaseWriter show all
Defined in:
lib/infold/writers/controller_writer.rb

Instance Attribute Summary collapse

Attributes inherited from BaseWriter

#resource

Instance Method Summary collapse

Methods inherited from BaseWriter

#edit_path, #indent, #index_path, #new_path, #resource_name, #show_path

Constructor Details

#initialize(resource) ⇒ ControllerWriter

Returns a new instance of ControllerWriter.



8
9
10
# File 'lib/infold/writers/controller_writer.rb', line 8

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#app_titleObject (readonly)

Returns the value of attribute app_title.



6
7
8
# File 'lib/infold/writers/controller_writer.rb', line 6

def app_title
  @app_title
end

Instance Method Details

#association_build_code(if_blank: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/infold/writers/controller_writer.rb', line 12

def association_build_code(if_blank: false)
  codes = []
  @resource.association_fields&.select { |af| !af.association.belongs_to? && af.form_element.present?  } &.each do |association_field|
    codes <<
      if association_field.association.has_one?
        "@#{resource_name(:snake)}.build_#{association_field.association.name}"
      else
        "@#{resource_name(:snake)}.#{association_field.association.name}.build"
      end
  end
  indent(codes.join("\n"), 3) if codes.present?
end

#post_params_codeObject



40
41
42
43
44
45
# File 'lib/infold/writers/controller_writer.rb', line 40

def post_params_code
  fields = post_params_fields(@resource.form_fields)
  fields = fields.join(",\n") if fields.present?
  code = "params.require(:admin_#{resource_name(:snake)}).permit(\n" + fields.to_s + "\n)"
  indent(code, 3) if fields.present?
end

#search_params_codeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/infold/writers/controller_writer.rb', line 25

def search_params_code
  fields = []
  any_fields = []
  @resource.conditions.each do |condition|
    if condition.sign == :any
      any_fields << "[TAB]#{condition.scope}: []"
    else
      fields << "[TAB]:#{condition.scope}"
    end
  end
  fields += %w([TAB]:sort_field [TAB]:sort_kind)
  code = "params[:search]&.permit(\n" + (fields + any_fields).uniq.join(",\n") + "\n)"
  indent(code, 3) if fields.present?
end