Module: Infold::ModelColumnDecorator
- Defined in:
- app/decorators/infold/model_column_decorator.rb
Instance Method Summary collapse
- #code_enum ⇒ Object
- #code_for_datetime_validates ⇒ Object
- #code_for_default ⇒ Object
- #code_validates ⇒ Object
- #default_value ⇒ Object
- #locale_and_comment ⇒ Object
- #method_for_boolean ⇒ Object
- #method_for_date(format = '%Y/%m/%d') ⇒ Object
- #method_for_datetime ⇒ Object
- #method_for_decorator ⇒ Object
- #method_for_enum ⇒ Object
- #method_for_number ⇒ Object
- #method_for_string ⇒ Object
- #set_unit(code) ⇒ Object
Instance Method Details
#code_enum ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'app/decorators/infold/model_column_decorator.rb', line 18 def code_enum return if !kind_enum? || enums.blank? enum_codes = enums.map do |enum| "#{enum.key}: #{kind_string? ? "'#{enum.value}'" : enum.value}" end code = "enum #{name}: { " code << enum_codes.join(', ') code << " }, _prefix: true\n" end |
#code_for_datetime_validates ⇒ Object
71 72 73 74 75 76 |
# File 'app/decorators/infold/model_column_decorator.rb', line 71 def code_for_datetime_validates return if !kind_datetime? || code = "validates :#{name}_date, presence: true, if: ->{ #{name}_time.present? }\n" code += " validates :#{name}_time, presence: true, if: ->{ #{name}_date.present? }" "#{code}" end |
#code_for_default ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/decorators/infold/model_column_decorator.rb', line 167 def code_for_default case kind.to_s when 'string' "'#{default}'" when 'boolean' if default.to_s == '0' 'false' elsif default.to_s == '1' 'true' else 'nil' end when 'number' default else 'nil' end end |
#code_validates ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/decorators/infold/model_column_decorator.rb', line 28 def code_validates return nil if is_primary_key? || if is_foreign_key? && required_validate? return "validates :#{model_association.belongs_name}, presence: true" end code = '' # 必須 if required_validate? code += ", presence: true" end # unique if unique? code += ', allow_blank: true' if code.blank? code += ", uniqueness: true" end # 数値 if kind_number? && !is_foreign_key? code += ', allow_blank: true' if code.blank? _code = ", numericality: { " _code << 'only_integer: true, ' if %w(integer bigint).include?(number_kind) _code << "greater_than_or_equal_to: #{minimum_value}, " if minimum_value.present? _code << "less_than_or_equal_to: #{maximum_value}, " if maximum_value.present? _code.gsub!(/ { $/, 'true') _code.gsub!(/, $/, ' }') code += _code end # 文字列 if kind_string? && (minimum_length.present? || maximum_length.present?) code += ', allow_blank: true' if code.blank? _code = "length: { " _code << "minimum: #{minimum_length}, " if minimum_length.present? _code << "maximum: #{maximum_length} }" if maximum_length.present? _code.gsub!(/, $/, ' }') code += _code end "validates :#{name}#{code}" if code.present? end |
#default_value ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'app/decorators/infold/model_column_decorator.rb', line 4 def default_value if default.blank? && !not_null? 'nil' elsif %w(boolean number enum date datetime).include?(kind) default.presence || 'nil' else "'#{default}'" end end |
#locale_and_comment ⇒ Object
14 15 16 |
# File 'app/decorators/infold/model_column_decorator.rb', line 14 def locale_and_comment [locale.presence, comment.presence].compact.join('.') end |
#method_for_boolean ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'app/decorators/infold/model_column_decorator.rb', line 95 def method_for_boolean <<"METHOD" def #{name}_display '<i class="bi bi-check-square-fill text-info"></i>'.html_safe if #{name}? end METHOD end |
#method_for_date(format = '%Y/%m/%d') ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'app/decorators/infold/model_column_decorator.rb', line 113 def method_for_date(format = '%Y/%m/%d') <<"METHOD" def #{name}_display #{name} ? I18n.l(#{name}) : '' end METHOD end |
#method_for_datetime ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'app/decorators/infold/model_column_decorator.rb', line 104 def method_for_datetime <<"METHOD" def #{name}_display #{name} ? I18n.l(#{name}) : '' end METHOD end |
#method_for_decorator ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/decorators/infold/model_column_decorator.rb', line 78 def method_for_decorator case kind.to_sym when :boolean method_for_boolean when :date method_for_date when :datetime method_for_datetime when :number delimited? || unit.present? ? method_for_number : nil when :enum method_for_enum else unit.present? ? method_for_string : nil end end |
#method_for_enum ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/decorators/infold/model_column_decorator.rb', line 133 def method_for_enum code = enums.map do |enum| enum.color.present? ? "when '#{enum.key}' then '#{enum.color}'" : nil end.compact return nil if code.blank? code = code.join("\n ") <<"METHOD" def #{name}_color case #{name}.to_s #{code} else '' end end METHOD end |
#method_for_number ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'app/decorators/infold/model_column_decorator.rb', line 122 def method_for_number code = delimited? ? "#{name}.to_formatted_s(:delimited)" : "#{name}.to_s" code = set_unit(code) <<"METHOD" def #{name}_display #{code} if #{name}.present? end METHOD end |
#method_for_string ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'app/decorators/infold/model_column_decorator.rb', line 150 def method_for_string <<"METHOD" def #{name}_display #{set_unit(name)} if #{name}.present? end METHOD end |
#set_unit(code) ⇒ Object
159 160 161 162 163 164 165 |
# File 'app/decorators/infold/model_column_decorator.rb', line 159 def set_unit(code) if unit.present? return "\"#{unit}\#{#{code}}\"" if unit_position_prefix? return "\"\#{#{code}}#{unit}\"" if unit_position_suffix? end code end |