Class: Schemaker::SubjectModel

Inherits:
BaseModel show all
Defined in:
lib/schemaker/models/subject_model.rb

Instance Attribute Summary collapse

Attributes inherited from BaseModel

#logs, #models, #my_class

Instance Method Summary collapse

Methods inherited from BaseModel

#clazz_name, model_types

Constructor Details

#initialize(models, clazz, main_field) ⇒ SubjectModel

Returns a new instance of SubjectModel.

Parameters:

  • each (Schema::Models)

    model needs to have access to the collection of models it may need to create relations with

  • reference (Class)

    to the Class it aims to configure!

  • the (Symbol)

    name of the main field for the behavior to add, fx :troles for adding roles behavior



17
18
19
20
# File 'lib/schemaker/models/subject_model.rb', line 17

def initialize models, clazz, main_field
  super models, clazz
  @main_field = main_field
end

Instance Attribute Details

#main_fieldObject

the main field for the behavior to add, fx :troles for adding roles behavior



12
13
14
# File 'lib/schemaker/models/subject_model.rb', line 12

def main_field
  @main_field
end

Instance Method Details

#configureObject

Example:

UserAccount (subject)        
  has_many :user_roles, :class_name => 'UserRole' (join)
  has_many :roles,      :class_name => 'Role', :through => :users_roles (subject)


31
32
33
34
# File 'lib/schemaker/models/subject_model.rb', line 31

def configure 
  super
  create_has_many_through :object, :key => main_field          
end

#quick_join(options = {}) ⇒ Object

Used to set up a ‘quick join’ using Rails conventions and ‘has_and_belongs_to_many’ on the subject and object



23
24
25
# File 'lib/schemaker/models/subject_model.rb', line 23

def quick_join options = {}
  create_has_and_belongs_to_many :object
end

#through_options(options = {}) ⇒ Object

Note:

important to use super to avoid recursive stack overflow!



37
38
39
# File 'lib/schemaker/models/subject_model.rb', line 37

def through_options options = {}
  options.merge super(:object).merge(foreign_key_option :subject)
end