Class: Schemaker::Models::SubjectModel

Inherits:
BaseModel
  • Object
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



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

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



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

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)


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

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



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

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!



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

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