module Schemaless::Fields::ClassMethods
Schemaless
ActiveRecord fields
Constants
- AR_OPTS
Public Instance Methods
belongs_to(*params)
click to toggle source
Create the belongs_to
foreign keys
Calls superclass method
# File lib/schemaless/ar/fields.rb, line 42 def belongs_to(*params) config = params.extract_options! name = "#{params.join}_id" schemaless_fields << ::Schemaless::Field.new(name, :integer, config) super(*params) end
current_attributes()
click to toggle source
Get all fields in a schemaless way
# File lib/schemaless/ar/fields.rb, line 65 def current_attributes columns_hash.map do |k, v| next if v.primary opts = AR_OPTS.reduce({}) do |a, e| v.send(e) ? a.merge(e => v.send(e)) : a end ::Schemaless::Field.new(k, v.type, opts) end.reject!(&:nil?) end
field(*params)
click to toggle source
field(*)
Gets all fields in the model in the schamless_fields array.
field :name # Defaults String field :cylinders, Integer # Use Class or :symbols field :type, index: true, default: nil, limit: 5
# File lib/schemaless/ar/fields.rb, line 29 def field(*params) config = params.extract_options! config.assert_valid_keys(*::Schemaless::Field::VALID_OPTS) type = config.delete(:type) type ||= params.size > 1 ? params.pop : :string # TBD name = params.join schemaless_fields << ::Schemaless::Field.new(name, type, config) end
schemaless_fields()
click to toggle source
# File lib/schemaless/ar/fields.rb, line 15 def schemaless_fields @schemaless_fields ||= [] end
timestamps(*params)
click to toggle source
Helper for timestamps
# File lib/schemaless/ar/fields.rb, line 53 def timestamps(*params) config = params.extract_options! type = config[:type] || :timestamp schemaless_fields << ::Schemaless::Field.new(:created_at, type, null: false) schemaless_fields << ::Schemaless::Field.new(:updated_at, type, null: true) return unless config[:paranoid] schemaless_fields << ::Schemaless::Field.new(:deleted_at, type, null: true) end