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_definitions_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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/declare_schema/model.rb', line 251 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::ForeignKeyDefinition.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 89 |
# File 'lib/declare_schema/model.rb', line 80 def declare_field(name, type, *args) = args. try(:field_added, name, type, args, ) add_serialize_for_field(name, type, ) add_formatting_for_field(name, type) 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 attr_order.include?(name) 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_definitions.any? { |index_spec| index_spec.fields == index_fields_s } index_definitions << ::DeclareSchema::Model::IndexDefinition.new(self, fields, ) end end |
#index_definitions_with_primary_key ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/declare_schema/model.rb', line 91 def index_definitions_with_primary_key if index_definitions.any?(&:primary_key?) index_definitions else index_definitions + [rails_default_primary_key] end end |
#primary_key ⇒ Object
99 100 101 |
# File 'lib/declare_schema/model.rb', line 99 def primary_key super || 'id' end |
#primary_key_index(*fields) ⇒ Object
59 60 61 |
# File 'lib/declare_schema/model.rb', line 59 def primary_key_index(*fields) index(fields.flatten, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME) end |