Class: Infold::ModelWriter

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

Instance Attribute Summary

Attributes inherited from BaseWriter

#resource

Instance Method Summary collapse

Methods inherited from BaseWriter

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

Constructor Details

This class inherits a constructor from Infold::BaseWriter

Instance Method Details

#accepts_nested_attributes_codeObject



19
20
21
22
23
24
25
26
27
# File 'lib/infold/writers/model_writer.rb', line 19

def accepts_nested_attributes_code
  code = ''
  @resource.associations&.
    select { |as| !as.belongs_to? && as.field.form_element.present?  }&.each do |association|
    code += "accepts_nested_attributes_for :#{association.name}, reject_if: :all_blank, allow_destroy: true\n"
    code += "validates_associated :#{association.name}\n"
  end
  indent(code, 2).presence
end

#active_storage_attachment_codeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/infold/writers/model_writer.rb', line 45

def active_storage_attachment_code
  code = ''
  @resource.active_storage_fields&.each do |active_storage_field|
    base = "has_one_attached :#{active_storage_field.name}"
    thumb = active_storage_field.active_storage.thumb
    if thumb
      code += <<-CODE.gsub(/^\s+/, '')
        #{base} do |attachable|
        [TAB]attachable.variant :thumb, resize_to_#{thumb.kind}: [#{thumb.width}, #{thumb.height}]
        end
      CODE
    else
      code += "#{base}\n"
    end
    code +=  <<-CODE.gsub(/^\s+/, '')
      attr_accessor :remove_#{active_storage_field.name}
      before_validation { self.#{active_storage_field.name} = nil if remove_#{active_storage_field.name}.to_s == '1' }
    CODE
    code += "\n"
  end
  indent(code, 2).presence
end

#association_codeObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/infold/writers/model_writer.rb', line 6

def association_code
  code = ''
  @resource.association_fields&.each do |association_field|
    association = association_field.association
    code += "#{association.kind} :#{association.name}"
    code += ", class_name: '#{association.class_name}'" if association.class_name.present?
    code += ", foreign_key: '#{association.foreign_key}'" if association.foreign_key.present?
    code += ", dependent: :#{association.dependent}" if association.dependent.present?
    code += "\n"
  end
  indent(code, 2).presence
end

#datetime_field_codeObject



29
30
31
32
33
34
35
# File 'lib/infold/writers/model_writer.rb', line 29

def datetime_field_code
  code = ''
  @resource.datetime_fields&.each do |field|
    code += "datetime_field :#{field.name}\n"
  end
  indent(code, 2).presence
end

#datetime_validation_codeObject



86
87
88
89
90
91
92
93
94
# File 'lib/infold/writers/model_writer.rb', line 86

def datetime_validation_code
  code = []
  @resource.datetime_fields&.each do |field|
    code << "validates :#{field.name}_date, presence: true, if: -> { #{field.name}_time.present? }"
    code << "validates :#{field.name}_time, presence: true, if: -> { #{field.name}_date.present? }"
  end
  code << "" if code.present?
  indent(code.join("\n"), 2).presence
end

#delegate_codeObject



37
38
39
40
41
42
43
# File 'lib/infold/writers/model_writer.rb', line 37

def delegate_code
  code = ''
  @resource.associations&.select { |as| as.belongs_to? && as.name_field != 'id' }&.each do |association|
    code += "delegate :#{association.name_field}, to: :#{association.name}, prefix: true, allow_nil: true\n"
  end
  indent(code, 2).presence
end

#enum_codeObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/infold/writers/model_writer.rb', line 96

def enum_code
  code = []
  @resource.enum_fields&.each do |enum_field|
    enum = enum_field.enum
    elements = enum.elements.map { |element| "#{element.key}: #{element.value}" }
    code << "enum #{enum_field.name}: { #{elements.join(', ')} }, _prefix: true"
  end
  code << "\n" if code.present?
  indent(code.join("\n"), 2).presence
end

#scope_codeObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/infold/writers/model_writer.rb', line 107

def scope_code
  code = ''
  @resource.condition_fields&.each do |field|
    field_name = field.name
    field.search_conditions.each do |condition|
      sign = condition.sign
      where =
        if field.type == :datetime && %i(eq full_like lteq).include?(sign)
          if sign == :lteq
            <<-CODE.gsub(/^\s+/, '')
              return if v.blank?
              [TAB]begin
              [TAB][TAB]v = v.to_date.next_day
              [TAB]rescue
              [TAB]end
              [TAB]where(arel_table[:#{field_name}].#{sign}(v))
            CODE
          else
            <<-CODE.gsub(/^\s+/, '')
              return if v.blank?
              [TAB]begin
              [TAB][TAB]where(#{field_name}: v.to_date.all_day)
              [TAB]rescue
              [TAB][TAB]where(arel_table[:#{field_name}].#{sign}(v))
              [TAB]end
            CODE
          end
        else
          case sign
          when :eq
            "where(#{field_name}: v) if v.present?"
          when :full_like
            "where(arel_table[:#{field_name}].matches(" +'"%#{v}%"' + ")) if v.present?"
          when :start_with
            "where(arel_table[:#{field_name}].matches(" +'"#{v}%"' + ")) if v.present?"
          when :any
            "where(#{field_name}: v) if v.present?"
          else
            "where(arel_table[:#{field_name}].#{sign}(v)) if v.present?"
          end
        end
      code +=  <<-CODE.gsub(/^\s+/, '')
        scope :#{field_name}_#{sign}, ->(v) do
        [TAB]#{where}
        end
      CODE
      code += "\n"
    end
  end
  indent(code, 2).presence
end

#validation_codeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/infold/writers/model_writer.rb', line 68

def validation_code
  code = ''
  @resource.validation_fields&.each do |validation_field|
    validation = validation_field.validation
    code += "validates :#{validation_field.name}, "
    code += "allow_blank: true, " unless validation.conditions.map(&:condition).include?(:presence)
    code += validation.conditions.map do |condition|
      if condition.options.blank?
        "#{condition.condition}: true"
      else
        "#{condition.condition}: { #{condition.options.map { |key, value| "#{key}: #{value}" }.join(', ')} }"
      end
    end.join(', ')
    code += "\n"
  end
  indent(code, 2).presence
end