Module: DynamicFields::Fields::ClassMethods
- Defined in:
- lib/dynamic_fields/fields.rb
Overview
:nodoc
Instance Method Summary collapse
-
#field(name, options = {}) ⇒ Object
Defines all the fields that are accessible on the model.
-
#field_names ⇒ Object
All field names (as defined in the model).
-
#index(field_or_fields, options = {}) ⇒ Object
Defines all the indices on the model.
-
#index_ids ⇒ Object
All index ids (as defined in the model).
- #migration_name ⇒ Object
-
#new_fields ⇒ Object
Any fields which have not been add to the table.
-
#new_indices ⇒ Object
Any indicies which have not been add to the table.
-
#old_fields ⇒ Object
Any fields in the table, but not in the model.
-
#old_indices ⇒ Object
Any indices in the table, but not in the model.
-
#real_field_names ⇒ Object
All pre-existing field names.
-
#real_fields ⇒ Object
All pre-existing fields.
-
#real_index_ids ⇒ Object
All pre-existing index ids.
-
#real_indices ⇒ Object
All pre-existing indices.
-
#requires_migration? ⇒ Boolean
Check in the model requires a migration (any new or old fields/indices?).
Instance Method Details
#field(name, options = {}) ⇒ Object
Defines all the fields that are accessible on the model
Options:
name: The name of the field, as a Symbol
. options: A Hash
of options to supply to the Field
.
Example:
field :score, :default => 0
28 29 30 |
# File 'lib/dynamic_fields/fields.rb', line 28 def field name, = {} fields << ::DynamicFields::Field.new(name, ) unless field_names.include?(name.to_s) end |
#field_names ⇒ Object
All field names (as defined in the model)
33 34 35 |
# File 'lib/dynamic_fields/fields.rb', line 33 def field_names fields.map(&:name).map(&:to_s) end |
#index(field_or_fields, options = {}) ⇒ Object
Defines all the indices on the model
Options:
name: The name of the index, as a String
. options: A Hash
of options to supply to the Index
.
Example:
index :user_id
index [:user_id, :user_group_id], :name => "user_groups", :unique => true
72 73 74 75 76 |
# File 'lib/dynamic_fields/fields.rb', line 72 def index field_or_fields, ={} [:name] = .delete(:as) || .delete(:name) || connection.index_name(table_name, :column => field_or_fields) idx = ::DynamicFields::Index.new(field_or_fields, ) indices << idx unless index_ids.include?(idx.id) end |
#index_ids ⇒ Object
All index ids (as defined in the model)
79 80 81 |
# File 'lib/dynamic_fields/fields.rb', line 79 def index_ids indices.map(&:id) end |
#migration_name ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/dynamic_fields/fields.rb', line 117 def migration_name return "create_#{table_name}" unless table_exists? "update_#{table_name}_" + [ new_fields_migration_name, new_indices_migration_name, old_fields_migration_name, old_indices_migration_name ].compact.join("_and_") end |
#new_fields ⇒ Object
Any fields which have not been add to the table
52 53 54 |
# File 'lib/dynamic_fields/fields.rb', line 52 def new_fields (self.fields ||= []).reject {|f| real_field_names.include?(f.name.to_s) } end |
#new_indices ⇒ Object
Any indicies which have not been add to the table
103 104 105 |
# File 'lib/dynamic_fields/fields.rb', line 103 def new_indices (self.indices ||= []).reject { |idx| real_index_ids.include?(idx.id) } end |
#old_fields ⇒ Object
Any fields in the table, but not in the model
57 58 59 |
# File 'lib/dynamic_fields/fields.rb', line 57 def old_fields real_fields.reject { |f| field_names.include?(f.name.to_s) } end |
#old_indices ⇒ Object
Any indices in the table, but not in the model
108 109 110 |
# File 'lib/dynamic_fields/fields.rb', line 108 def old_indices real_indices.reject { |idx| index_ids.include?(idx.id) } end |
#real_field_names ⇒ Object
All pre-existing field names
38 39 40 |
# File 'lib/dynamic_fields/fields.rb', line 38 def real_field_names table_exists? ? column_names : [] end |
#real_fields ⇒ Object
All pre-existing fields
43 44 45 46 47 48 49 |
# File 'lib/dynamic_fields/fields.rb', line 43 def real_fields return [] unless table_exists? columns.map do |c| next if c.primary == true ::DynamicFields::Field.new(c.name, :type => c.type, :default => c.default) end.compact end |
#real_index_ids ⇒ Object
All pre-existing index ids
84 85 86 |
# File 'lib/dynamic_fields/fields.rb', line 84 def real_index_ids real_indices.map(&:id) end |
#real_indices ⇒ Object
All pre-existing indices
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dynamic_fields/fields.rb', line 89 def real_indices return [] unless table_exists? unless connection.respond_to?(:indexes) p "Dynamic indices not supported - remove old indices manually" return [] else connection.indexes(table_name).map do |idx| idxs = idx.columns.size > 1 ? idx.columns : idx.columns.first ::DynamicFields::Index.new(idxs, {:name => idx.name, :unique => idx.unique}) end.compact end end |
#requires_migration? ⇒ Boolean
Check in the model requires a migration (any new or old fields/indices?)
113 114 115 |
# File 'lib/dynamic_fields/fields.rb', line 113 def requires_migration? (new_fields + new_indices + old_fields + old_indices).any? end |