Module: DeclareSchema::Model::ClassMethods
- Defined in:
- lib/declare_schema/model.rb
Instance Method Summary collapse
-
#attr_type(name) ⇒ Object
Returns the type (a class) for a given field or association.
- #constraint(fkey, options = {}) ⇒ Object
-
#declare_field(name, type, *args) ⇒ Object
Declare named field with a type and an arbitrary set of arguments.
-
#ignore_index(index_name) ⇒ Object
tell the migration generator to ignore the named index.
- #index(fields, options = {}) ⇒ Object
- #index_specs_with_primary_key ⇒ Object
- #primary_key ⇒ Object
- #primary_key_index(*fields) ⇒ Object
Instance Method Details
#attr_type(name) ⇒ Object
Returns the type (a class) for a given field or association. If the association is a collection (has_many or habtm) return the AssociationReflection instead
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/declare_schema/model.rb', line 214 public \ def attr_type(name) if attr_types.nil? && self != self.name.constantize raise "attr_types called on a stale class object (#{self.name}). Avoid storing persistent references to classes" end attr_types[name] || if (refl = reflections[name.to_s]) if refl.macro.in?([:has_one, :belongs_to]) && !refl.[:polymorphic] refl.klass else refl end end || if (col = column(name.to_s)) DeclareSchema::PLAIN_TYPES[col.type] || col.klass end end |
#constraint(fkey, options = {}) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/declare_schema/model.rb', line 63 def constraint(fkey, = {}) fkey_s = fkey.to_s unless constraint_specs.any? { |constraint_spec| constraint_spec.foreign_key == fkey_s } constraint_specs << DeclareSchema::Model::ForeignKeySpec.new(self, fkey, ) end end |
#declare_field(name, type, *args) ⇒ Object
Declare named field with a type and an arbitrary set of arguments. The arguments are forwarded to the #field_added callback, allowing custom metadata to be added to field declarations.
80 81 82 83 84 85 86 87 88 |
# File 'lib/declare_schema/model.rb', line 80 def declare_field(name, type, *args) = args. field_added(name, type, args, ) if respond_to?(:field_added) add_formatting_for_field(name, type, args) add_validations_for_field(name, type, args, ) add_index_for_field(name, args, ) field_specs[name] = ::DeclareSchema::Model::FieldSpec.new(self, name, type, ) attr_order << name unless name.in?(attr_order) end |
#ignore_index(index_name) ⇒ Object
tell the migration generator to ignore the named index. Useful for existing indexes, or for indexes that can't be automatically generated (for example: an prefix index in MySQL)
72 73 74 |
# File 'lib/declare_schema/model.rb', line 72 def ignore_index(index_name) ignore_indexes << index_name.to_s end |
#index(fields, options = {}) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/declare_schema/model.rb', line 51 def index(fields, = {}) # don't double-index fields index_fields_s = Array.wrap(fields).map(&:to_s) unless index_specs.any? { |index_spec| index_spec.fields == index_fields_s } index_specs << ::DeclareSchema::Model::IndexSpec.new(self, fields, ) end end |
#index_specs_with_primary_key ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/declare_schema/model.rb', line 90 def index_specs_with_primary_key if index_specs.any?(&:primary_key?) index_specs else index_specs + [rails_default_primary_key] end end |
#primary_key ⇒ Object
98 99 100 |
# File 'lib/declare_schema/model.rb', line 98 def primary_key super || 'id' end |