Class: FReCon::Model
Overview
Public: A base class designed to assist with creating MongoDB Models elsewhere in the project.
Direct Known Subclasses
Class Method Summary collapse
-
.controller ⇒ Object
Public: Converts this Model to its associated Controller.
-
.descendants ⇒ Object
Public: Gets the descendants for the Model class.
-
.inherited(child) ⇒ Object
Public: Bootstraps inheritors of this class as working Models, also providing class methods for them to use.
Instance Method Summary collapse
-
#no_invalid_relations ⇒ Object
Public: Validate that no invalid relations exist within this Model.
Class Method Details
.controller ⇒ Object
Public: Converts this Model to its associated Controller.
Returns the associated Controller if it exists, else nil.
88 89 90 |
# File 'lib/frecon/model.rb', line 88 def self.controller (self.name.pluralize + 'Controller').constantize end |
.descendants ⇒ Object
Public: Gets the descendants for the Model class.
Returns all of the descendants for the Model class.
79 80 81 82 83 |
# File 'lib/frecon/model.rb', line 79 def self.descendants ObjectSpace.each_object(Class).select do |possible_child| possible_child < self end end |
.inherited(child) ⇒ Object
Public: Bootstraps inheritors of this class as working Models, also providing class methods for them to use.
child - The class that is inheriting this class.
Returns the result of bootstrapping the child.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/frecon/model.rb', line 25 def self.inherited(child) child.class_eval do # Include the various Mongoid modules that we want to use. include Mongoid::Document include Mongoid::Timestamps include Mongoid::Attributes::Dynamic # Ensure that no invalid relations exist. validate :no_invalid_relations self.class_variable_set(:@@attributes, []) # Public: Register a method as a routable relation method. # # Models can register relation methods that they have defined # (e.g. team.robots) as routable methods. The Routes module reads # these routable relations, and generates routes for them. # # method - A Symbol containing the name of the relation method. # attribute - A String representing the attribute that the Routes # module should route this method under. # # Examples # # # (Taken from the Team model) # register_routable_relation :matches, 'matches' # # Returns the result of pushing an object to class's attributes # class variable. def self.register_routable_relation(method, attribute) self.class_variable_get(:@@attributes) << {method: method, type: :relation, attribute: attribute} end # Public: Register a method as a routable attribute method. # # Models can register attribute methods that they have defined # (e.g. team.number) as attribute methods. The Routes module reads # these routable attributes, and generates routes for them. # # method - A Symbol containing the name of the attribute method. # attribute - A String representing the attribute that the Routes # module should route this method under. # # Returns the result of pushing an object to class's attributes # class variable. def self.register_routable_attribute(method, attribute) self.class_variable_get(:@@attributes) << {method: method, type: :attribute, attribute: attribute} end end end |
Instance Method Details
#no_invalid_relations ⇒ Object
Public: Validate that no invalid relations exist within this Model
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/frecon/model.rb', line 93 def no_invalid_relations # Get all of the belongs_to fields (ends with '_id' and not '_id' because that is the id). attributes.keys.select do |attribute| attribute.end_with?('_id') && attribute != '_id' end.each do |relation| # Get the model for the belongs_to association. model = 'FReCon::'.concat(relation.gsub(/_id\Z/, '').capitalize).constantize errors.add(relation.to_sym, 'is invalid') if relation_invalid?(model, send(relation)) end end |