Module: DynamicFields::Fields::ClassMethods

Defined in:
lib/dynamic_fields/fields.rb

Overview

:nodoc

Instance Method Summary collapse

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, options = {}
  fields << ::DynamicFields::Field.new(name, options) unless field_names.include?(name.to_s)
end

#field_namesObject

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, options={}
  options[:name] = options.delete(:as) || options.delete(:name) || connection.index_name(table_name, :column => field_or_fields)
  idx = ::DynamicFields::Index.new(field_or_fields, options)
  indices << idx unless index_ids.include?(idx.id)
end

#index_idsObject

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_nameObject



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_fieldsObject

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_indicesObject

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_fieldsObject

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_indicesObject

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_namesObject

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_fieldsObject

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_idsObject

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_indicesObject

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?)

Returns:

  • (Boolean)


113
114
115
# File 'lib/dynamic_fields/fields.rb', line 113

def requires_migration?
  (new_fields + new_indices + old_fields + old_indices).any?
end