class Schemaker::Models::BaseModel
Attributes
Public Class Methods
# File lib/schemaker/models/base_model.rb, line 38 def self.model_types [:object, :subject, :join] end
@param [Schema::Models] each model needs to have access to the collection of models it may need to create relations with @param [Class] each model must have a reference to the Class it aims to configure!
# File lib/schemaker/models/base_model.rb, line 16 def initialize models, my_class raise ArgumentError, "The first argument must be a Schema::Models instance, was #{models}" if !models.is_a?(Schemaker::Models) raise ArgumentError, "The second argument must be the Class that is to be configured, was #{my_class}" if !my_class.is_a?(Class) @models = models @my_class = my_class @logs = [] end
Public Instance Methods
The class name of the Class to be configured @return [String] class name
# File lib/schemaker/models/base_model.rb, line 33 def clazz_name my_class.to_s end
The models :subject and :object must both be configured with a has_many relationship to the join model which the has_many :through references
# File lib/schemaker/models/base_model.rb, line 27 def configure create_has_many :join, class_name_option(join_class) end
Protected Instance Methods
sets up the :class_name relationship option for a given class (model @param [Class, String] the class to point to
# File lib/schemaker/models/base_model.rb, line 164 def class_name_option cls_name model = get_class(cls_name) {:class_name => model.to_s } end
Create a ‘belongs_to’ relationship on the model (Class) Example:
Role.belongs_to :user, :class_name => 'User'
# File lib/schemaker/models/base_model.rb, line 73 def create_belongs_to clazz, options = {} make_relationship :belongs_to, clazz, options.merge(:key => singular_key(clazz)) end
To setup symmetrical has_and_belongs_to_many relationship:
Example:
class UserAccount < ActiveRecord::Base has_and_belongs_to_many :troles, :class_name => 'Role' end class Role < ActiveRecord::Base has_and_belongs_to_many :user_accounts, :class_name => 'User' end
# File lib/schemaker/models/base_model.rb, line 110 def create_has_and_belongs_to_many to_clazz, options = {} make_relationship :has_and_belongs_to_many, to_clazz, options.merge(:key => singular_key(to_clazz)) models.object_model.make_relationship :has_and_belongs_to_many, my_class, options.merge(:key => singular_key(my_class)) end
Create a ‘has_many’ relationship on the model (Class) Example:
User.has_many :roles, :class_name => 'Role'
# File lib/schemaker/models/base_model.rb, line 80 def create_has_many clazz, options = {} make_relationship :has_many, clazz, options end
Create a ‘has_many :through’ relationship on the model (Class) Example:
User.has_many :roles, :class_name => 'Role', :through => 'UsersRoles'
# File lib/schemaker/models/base_model.rb, line 94 def create_has_many_through clazz, options = {} create_has_many clazz, through_options(options) end
Create a ‘has_many’ relationship on the model (Class) Example:
User.has_one :roles, :class_name => 'Role'
# File lib/schemaker/models/base_model.rb, line 87 def create_has_one clazz, options = {} make_relationship :has_one, clazz, options.merge(:key => singular_key(clazz)) end
creates the foreign key RefManyAccount becomes :account_id
# File lib/schemaker/models/base_model.rb, line 198 def foreign_key cls_name model = get_class(cls_name) name = model.to_s.underscore.split('_').last.singularize :"#{name}_id" end
sets up the :foreign_key relationship option the foreign key name should always correspond to ‘my own’ class name
# File lib/schemaker/models/base_model.rb, line 176 def foreign_key_option cls_name model = get_class(cls_name) {:foreign_key => foreign_key(model) } end
# File lib/schemaker/models/base_model.rb, line 135 def get_class type models.get_class type end
creates a key for a given type @param type [Symbol] - either :object, :subject or :join
# File lib/schemaker/models/base_model.rb, line 131 def key type models.key type end
log the relationship being added
-
to STDOUT via puts
-
to a logs list
# File lib/schemaker/models/base_model.rb, line 151 def log msg puts msg logs << msg end
Is global logging turned on
# File lib/schemaker/models/base_model.rb, line 66 def log_on? Schemaker.log_on? end
# File lib/schemaker/models/base_model.rb, line 144 def make_key cls_name models.make_key cls_name end
Creates a given type of relationship @param [Symbol] the type of relationship, fx :has_many @param [Class, Symbol] the Class that is the object of the relationship, fx Role for a User.has_many relationship @param [Hash] any extra relationship options, fx for a :through relationship, or to indicate :class_name etc.
# File lib/schemaker/models/base_model.rb, line 119 def make_relationship relationship_name, clazz, options = {} # inspect!(binding) key_name = (options.delete(:key) || key(clazz)).to_sym # first must be a sym too opts_str = options.empty? ? '' : options.inspect.insert(0, ', ').gsub(/[{}]/ , '') log "#{my_class_name}.#{relationship_name} :#{key_name}#{opts_str}" if log_on? return my_class.send(relationship_name, key_name) if options.empty? my_class.send(relationship_name, key_name, options) end
# File lib/schemaker/models/base_model.rb, line 139 def singular_key cls_name model = get_class(cls_name) model.to_s.singularize.underscore end
creates the source Role becomes :role
# File lib/schemaker/models/base_model.rb, line 191 def source cls_name model = get_class(cls_name) model.to_s.underscore.singularize.to_sym end
sets up the :source relationship option, typically for a has_many through relationship
# File lib/schemaker/models/base_model.rb, line 157 def source_option cls_name model = get_class(cls_name) {:source => source(model) } end
sets up the :through relationship option, always points to the join model
# File lib/schemaker/models/base_model.rb, line 170 def through_option {:through => join_model.through_key } end
sets up the full :through relationship options Example:
:class_name => 'Role', :through => 'UsersRoles', :source => :role, :foreign_key => :user_id)
# File lib/schemaker/models/base_model.rb, line 184 def through_options cls_name model = get_class(cls_name) through_option.merge(source_option model).merge(class_name_option model) end