class Mongify::Mongoid::Model
Class that will be used to define a mongoid model
Constants
- CREATED_AT_FIELD
default created at field name
- EXCLUDED_FIELDS
List of fields to exclude from the model
- UPDATED_AT_FIELD
default update at field name
Attributes
Public Class Methods
# File lib/mongify/mongoid/model.rb, line 48 def initialize(options = {}) @class_name = options[:class_name].to_s @table_name = options[:table_name].to_s self.polymorphic_as = options[:polymorphic_as] @fields = {} @relations = [] end
Public Instance Methods
Adds a field definition to the class, e.g:
add_field("field_name", "String", {rename_to: "name"})
# File lib/mongify/mongoid/model.rb, line 61 def add_field(name, type, options={}) options.stringify_keys! return if options['ignore'] || options['references'] || polymorphic_field?(name) check_for_timestamp(name) return if EXCLUDED_FIELDS.include?(name.to_s.downcase) name = options['rename_to'] if options['rename_to'].present? type = options['as'] if options['as'].present? begin @fields[name.to_sym] = Field.new(name, type, options) rescue InvalidField => e raise InvalidField, "Unknown field type #{type} for #{name} in #{class_name}" end end
Adds a relationship definition to the class, e.g:
add_relation("embedded_in", "users")
@note Embedded relations will overpower related relations @return [Relation] New generated relation
# File lib/mongify/mongoid/model.rb, line 81 def add_relation(relation_name, association, options={}) if existing = find_relation_by(association) if relation_name =~ /^embed/ delete_relation_for association else return end end Relation.new(relation_name.to_s, association, options).tap do |r| @relations << r end end
Returns binding for ERB template @return [Binding]
# File lib/mongify/mongoid/model.rb, line 96 def get_binding return binding() end
Returns true both timestamps are present (created_at and updated_at)
# File lib/mongify/mongoid/model.rb, line 29 def has_both_timestamps? has_created_at_timestamp? && has_updated_at_timestamp? end
Returns true created_at timestamp exists
# File lib/mongify/mongoid/model.rb, line 34 def has_created_at_timestamp? !!@created_at end
Returns true if it has any timestamps
# File lib/mongify/mongoid/model.rb, line 24 def has_timestamps? has_created_at_timestamp? || has_updated_at_timestamp? end
Returns true updated_at timestamp exists
# File lib/mongify/mongoid/model.rb, line 39 def has_updated_at_timestamp? !!@updated_at end
Returns true it is a polymorphic model
# File lib/mongify/mongoid/model.rb, line 44 def polymorphic? !!@polymorphic_as end
Returns an improved inspection output including fields and relations @return [String] String representation of the model
# File lib/mongify/mongoid/model.rb, line 102 def to_s "#<Mongify::Mongoid::Model::#{name} fields=#{@fields.keys} relations=#{@relations.map{|r| "#{r.name} :#{r.association}"}}>" end
Private Instance Methods
Checks if given field name is a known timestamp field @return [nil]
# File lib/mongify/mongoid/model.rb, line 112 def check_for_timestamp name @created_at = true if name == CREATED_AT_FIELD @updated_at = true if name == UPDATED_AT_FIELD nil end
Deletes given relations based on association name @return [nil]
# File lib/mongify/mongoid/model.rb, line 133 def delete_relation_for association @relations.reject!{ |r| r.association == association || r.association == association.singularize} end
Finds a relation by association @return [Relation, nil] The found relation or nil
# File lib/mongify/mongoid/model.rb, line 127 def find_relation_by association @relations.find{|r| r.association == association || r.association == association.singularize} end
Returns true if given field name follows polymorphic rules @return [Boolean]
# File lib/mongify/mongoid/model.rb, line 120 def polymorphic_field? name return unless polymorphic? name == "#{polymorphic_as}_type" || name == "#{polymorphic_as}_id" end