Class: Gris::Generators::GeneratedAttribute
- Inherits:
-
Object
- Object
- Gris::Generators::GeneratedAttribute
- Defined in:
- lib/gris/generators/migration_generator.rb
Overview
Constant Summary collapse
- INDEX_OPTIONS =
:nodoc:
%w(index uniq).freeze
- UNIQ_INDEX_OPTIONS =
%w(uniq).freeze
Instance Attribute Summary collapse
-
#attr_options ⇒ Object
readonly
Returns the value of attribute attr_options.
- #index_name ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #column_name ⇒ Object
- #default ⇒ Object
- #field_type ⇒ Object
- #foreign_key? ⇒ Boolean
- #has_index? ⇒ Boolean
- #has_uniq_index? ⇒ Boolean
- #human_name ⇒ Object
-
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
constructor
A new instance of GeneratedAttribute.
- #inject_index_options ⇒ Object
- #inject_options ⇒ Object
- #password_digest? ⇒ Boolean
- #plural_name ⇒ Object
- #polymorphic? ⇒ Boolean
- #reference? ⇒ Boolean
- #singular_name ⇒ Object
Constructor Details
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
Returns a new instance of GeneratedAttribute.
136 137 138 139 140 141 142 |
# File 'lib/gris/generators/migration_generator.rb', line 136 def initialize(name, type = nil, index_type = false, = {}) @name = name @type = type || :string @has_index = INDEX_OPTIONS.include?(index_type) @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type) @attr_options = end |
Instance Attribute Details
#attr_options ⇒ Object (readonly)
Returns the value of attribute attr_options.
88 89 90 |
# File 'lib/gris/generators/migration_generator.rb', line 88 def @attr_options end |
#index_name ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/gris/generators/migration_generator.rb', line 186 def index_name @index_name ||= if polymorphic? %w(id type).map { |t| "#{name}_#{t}" } else column_name end end |
#name ⇒ Object
Returns the value of attribute name.
87 88 89 |
# File 'lib/gris/generators/migration_generator.rb', line 87 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
87 88 89 |
# File 'lib/gris/generators/migration_generator.rb', line 87 def type @type end |
Class Method Details
.parse(column_definition) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gris/generators/migration_generator.rb', line 92 def parse(column_definition) name, type, has_index = column_definition.split(':') # if user provided "name:index" instead of "name:string:index" # type should be set blank so GeneratedAttribute's constructor # could set it to :string if INDEX_OPTIONS.include?(type) has_index = type type = nil end type, = *(type) type = type.to_sym if type if type && reference?(type) references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true [:index] = references_index end new(name, type, has_index, ) end |
.reference?(type) ⇒ Boolean
114 115 116 |
# File 'lib/gris/generators/migration_generator.rb', line 114 def reference?(type) [:references, :belongs_to].include? type end |
Instance Method Details
#column_name ⇒ Object
194 195 196 |
# File 'lib/gris/generators/migration_generator.rb', line 194 def column_name @column_name ||= reference? ? "#{name}_id" : name end |
#default ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/gris/generators/migration_generator.rb', line 158 def default @default ||= case type when :integer then 1 when :float then 1.5 when :decimal then '9.99' when :datetime, :timestamp, :time then Time.now.to_s(:db) when :date then Date.today.to_s(:db) when :string then name == 'type' ? '' : 'MyString' when :text then 'MyText' when :boolean then false when :references, :belongs_to then nil else '' end end |
#field_type ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gris/generators/migration_generator.rb', line 144 def field_type @field_type ||= case type when :integer then :number_field when :float, :decimal then :text_field when :time then :time_select when :datetime, :timestamp then :datetime_select when :date then :date_select when :text then :text_area when :boolean then :check_box else :text_field end end |
#foreign_key? ⇒ Boolean
198 199 200 |
# File 'lib/gris/generators/migration_generator.rb', line 198 def foreign_key? !!(name =~ /_id$/) end |
#has_index? ⇒ Boolean
210 211 212 |
# File 'lib/gris/generators/migration_generator.rb', line 210 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
214 215 216 |
# File 'lib/gris/generators/migration_generator.rb', line 214 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
182 183 184 |
# File 'lib/gris/generators/migration_generator.rb', line 182 def human_name name.humanize end |
#inject_index_options ⇒ Object
226 227 228 |
# File 'lib/gris/generators/migration_generator.rb', line 226 def has_uniq_index? ? ', unique: true' : '' end |
#inject_options ⇒ Object
222 223 224 |
# File 'lib/gris/generators/migration_generator.rb', line 222 def ''.tap { |s| @attr_options.each { |k, v| s << ", #{k}: #{v.inspect}" } } end |
#password_digest? ⇒ Boolean
218 219 220 |
# File 'lib/gris/generators/migration_generator.rb', line 218 def password_digest? name == 'password' && type == :digest end |
#plural_name ⇒ Object
174 175 176 |
# File 'lib/gris/generators/migration_generator.rb', line 174 def plural_name name.sub(/_id$/, '').pluralize end |
#polymorphic? ⇒ Boolean
206 207 208 |
# File 'lib/gris/generators/migration_generator.rb', line 206 def polymorphic? .key?(:polymorphic) end |
#reference? ⇒ Boolean
202 203 204 |
# File 'lib/gris/generators/migration_generator.rb', line 202 def reference? self.class.reference?(type) end |
#singular_name ⇒ Object
178 179 180 |
# File 'lib/gris/generators/migration_generator.rb', line 178 def singular_name name.sub(/_id$/, '').singularize end |