Class: Rails::Generators::GeneratedAttribute
- Defined in:
- railties/lib/rails/generators/generated_attribute.rb
Instance Attribute Summary collapse
-
#attr_options ⇒ Object
readonly
Returns the value of attribute attr_options.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #default ⇒ Object
- #field_type ⇒ Object
- #has_index? ⇒ Boolean
- #has_uniq_index? ⇒ Boolean
- #human_name ⇒ Object
- #index_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
- #reference? ⇒ Boolean
Constructor Details
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
Returns a new instance of GeneratedAttribute.
40 41 42 43 44 45 46 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 40 def initialize(name, type=nil, index_type=false, ={}) @name = name @type = (type.presence || :string).to_sym @has_index = %w(index uniq).include?(index_type) @has_uniq_index = %w(uniq).include?(index_type) @attr_options = end |
Instance Attribute Details
#attr_options ⇒ Object (readonly)
Returns the value of attribute attr_options
9 10 11 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 9 def @attr_options end |
#name ⇒ Object
Returns the value of attribute name
8 9 10 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 8 def name @name end |
#type ⇒ Object
Returns the value of attribute type
8 9 10 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 8 def type @type end |
Class Method Details
.parse(column_definition) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 12 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 has_index, type = type, nil if %w(index uniq).include?(type) type, = *(type) new(name, type, has_index, ) end |
Instance Method Details
#default ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 62 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
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 48 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 |
#has_index? ⇒ Boolean
90 91 92 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 90 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
94 95 96 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 94 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
78 79 80 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 78 def human_name name.to_s.humanize end |
#index_name ⇒ Object
82 83 84 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 82 def index_name reference? ? "#{name}_id" : name end |
#inject_index_options ⇒ Object
102 103 104 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 102 def has_uniq_index? ? ", :unique => true" : '' end |
#inject_options ⇒ Object
98 99 100 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 98 def "".tap { |s| @attr_options.each { |k,v| s << ", :#{k} => #{v.inspect}" } } end |
#reference? ⇒ Boolean
86 87 88 |
# File 'railties/lib/rails/generators/generated_attribute.rb', line 86 def reference? self.type.in?([:references, :belongs_to]) end |