Module: LucidWorks::Associations::ClassMethods
- Defined in:
- lib/lucid_works/associations.rb
Instance Method Summary collapse
-
#associations ⇒ Object
:nodoc:.
-
#belongs_to(parent_resource, options = {}) ⇒ Object
Specified a parent resource.
-
#has_many(*arguments) ⇒ Object
Specifies a collection child resource.
-
#has_one(*arguments) ⇒ Object
Specifies a singleton child resource.
Instance Method Details
#associations ⇒ Object
:nodoc:
11 12 13 |
# File 'lib/lucid_works/associations.rb', line 11 def associations #:nodoc: @associations ||= {} end |
#belongs_to(parent_resource, options = {}) ⇒ Object
Specified a parent resource. In the child resource creates: e.g. for Post belongs_to Blog
Class methods:
parent_class => Blog
Instance methods:
blog
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lucid_works/associations.rb', line 88 def belongs_to(parent_resource, = {}) parent_resource_class_name = ([:class_name] || parent_resource).to_s.classify class_eval <<-EOF, __FILE__, __LINE__ + 1 def self.parent_class # def self.parent_class #{parent_resource_class_name} # Blog end # end def #{parent_resource} # def blog parent # parent end # end private def self.belongs_to_association_name :'#{parent_resource}' end EOF end |
#has_many(*arguments) ⇒ Object
Specifies a collection child resource.
e.g. for Blog has_many posts
In the parent resources creates methods:
posts
post(id)
72 73 74 75 76 77 |
# File 'lib/lucid_works/associations.rb', line 72 def has_many(*arguments) = arguments.last.is_a?(Hash) ? arguments.pop : {} arguments.each do |resources| define_has_many resources, end end |
#has_one(*arguments) ⇒ Object
Specifies a singleton child resource.
has_one :resource, <options>
In the parent resource, creates methods:
.resource
.resource!
.resource.build(attributes={})
.resource.create(attributes={})
.resource.create!(attributes={})
.build_resource(attributes={})
.create_resource(attributes={})
.resource will not actually load the resource until used i.e. resource.anything is called. After being loaded, the child is cached. Use .resource! or resource.reload! to relaod it.
.resource! will load the resource immediately, or reload it if it was already loaded.
.build creates a new model but does not save it. Parent is preset to the calling class.
.create builds the model, saves it, then returns it. You need to check persisted? and errors to see if the save succeeded.
.build_resource and .create_resource are aliases for .resource.build and .resource.create respectively.
Options
The declaration can also include an options hash to specialize the behavior of the association.
Options are:
- :class_name => “name”
-
Specify the class name of the association. Use it if you want to us a child class name different from the association name, e.g.
has_one :foo # Use class Foo has_one :foo, :class_name => :'things/foo_bar' # use class Things::FooBar
- :has_content => false
-
If set to false, indicated that this resource may not be retrieved using a GET, and will cause the .resource method to return nil until the resource is built or created. This is useful for pseudo-resources that only provide actions, not data.
56 57 58 59 60 61 |
# File 'lib/lucid_works/associations.rb', line 56 def has_one(*arguments) = arguments.last.is_a?(Hash) ? arguments.pop : {} arguments.each do |resource| define_has_one resource, end end |