Module: ActiveRecordSchema::Dsl::ClassMethods
- Defined in:
- lib/active_record_schema/dsl.rb
Instance Method Summary collapse
- #belongs_to(name, options = {}) ⇒ Object
-
#field(name, *args) ⇒ Object
field :name, :string, :default => “Joe”.
- #has_and_belongs_to_many(name, options = {}, &extension) ⇒ Object
- #index(column_name, options = {}) ⇒ Object (also: #add_index)
- #inheritable ⇒ Object
- #schema ⇒ Object
- #timestamps ⇒ Object
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_record_schema/dsl.rb', line 27 def belongs_to(name, = {}) .symbolize_keys! skip_index = .delete(:index) == false foreign_key = [:foreign_key] || "#{name}_id" field :"#{foreign_key}", :as => :integer if [:polymorphic] foreign_type = [:foreign_type] || "#{name}_type" field :"#{foreign_type}" add_index [:"#{foreign_key}", :"#{foreign_type}"] if !skip_index else add_index :"#{foreign_key}" if !skip_index end super(name, ) end |
#field(name, *args) ⇒ Object
field :name, :string, :default => “Joe”
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_record_schema/dsl.rb', line 14 def field(name, *args) = args. type = .delete(:as) || .delete(:type) || args.first || :string type = type.name.underscore.to_sym if (type.class == Class) index = .delete(:index) schema.add_field(name, type, ) if index schema.add_index(name) end end |
#has_and_belongs_to_many(name, options = {}, &extension) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record_schema/dsl.rb', line 45 def has_and_belongs_to_many(name, = {}, &extension) .symbolize_keys! self_class_name = self.name association_class_name = [:class_name] || "#{name}".singularize.camelize table = [:join_table] || [self_class_name, association_class_name].sort.map(&:tableize).join("_") key1 = [:foreign_key] || "#{self_class_name.underscore}_id" key2 = [:association_foreign_key] || "#{association_class_name.underscore}_id" skip_index = .delete(:index) == false schema.add_join(table, key1, key2, !skip_index) super(name, , &extension) end |
#index(column_name, options = {}) ⇒ Object Also known as: add_index
60 61 62 |
# File 'lib/active_record_schema/dsl.rb', line 60 def index(column_name, = {}) schema.add_index(column_name, ) end |
#inheritable ⇒ Object
70 71 72 |
# File 'lib/active_record_schema/dsl.rb', line 70 def inheritable field :"#{inheritance_column}" end |
#schema ⇒ Object
8 9 10 11 |
# File 'lib/active_record_schema/dsl.rb', line 8 def schema @active_record_schema_schema ||= ActiveRecordSchema::Schema.new(self) end |
#timestamps ⇒ Object
65 66 67 68 |
# File 'lib/active_record_schema/dsl.rb', line 65 def field :created_at, :datetime field :updated_at, :datetime end |