class Schemaker::Models::BaseModel

Attributes

logs[R]
models[RW]
my_class[RW]

Public Class Methods

model_types() click to toggle source
# File lib/schemaker/models/base_model.rb, line 38
def self.model_types
  [:object, :subject, :join]
end
new(models, my_class) click to toggle source

@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

clazz_name() click to toggle source

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
Also aliased as: my_class_name
configure() click to toggle source

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
my_class_name()
Alias for: clazz_name

Protected Instance Methods

class_name_option(cls_name) click to toggle source

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_belongs_to(clazz, options = {}) click to toggle source

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
create_has_and_belongs_to_many(to_clazz, options = {}) click to toggle source

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_has_many(clazz, options = {}) click to toggle source

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_has_many_through(clazz, options = {}) click to toggle source

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_has_one(clazz, options = {}) click to toggle source

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
foreign_key(cls_name) click to toggle source

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
foreign_key_option(cls_name) click to toggle source

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
get_class(type) click to toggle source
# File lib/schemaker/models/base_model.rb, line 135
def get_class type
  models.get_class type
end
key(type) click to toggle source

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(msg) click to toggle source

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
log_on?() click to toggle source

Is global logging turned on

# File lib/schemaker/models/base_model.rb, line 66
def log_on?
  Schemaker.log_on?
end
make_key(cls_name) click to toggle source
# File lib/schemaker/models/base_model.rb, line 144
def make_key cls_name
  models.make_key cls_name
end
make_relationship(relationship_name, clazz, options = {}) click to toggle source

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
singular_key(cls_name) click to toggle source
# 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
source(cls_name) click to toggle source

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
source_option(cls_name) click to toggle source

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
through_option() click to toggle source

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
through_options(cls_name) click to toggle source

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