Module: Resourcify::ParentRelationship
- Defined in:
- lib/resourcify/parent_relationship.rb
Class Method Summary collapse
Instance Method Summary collapse
- #collection(query = {}) ⇒ Object
- #owner ⇒ Object
- #parent ⇒ Object
- #parent_key ⇒ Object
- #parent_relationship ⇒ Object
- #parent_relationship? ⇒ Boolean
- #relationship_collections ⇒ Object
Class Method Details
.included(base) ⇒ Object
e.g. class Person
belongs_to :location
end
class Location
has n, :people
end
access_by :location
(Options) :collection => :person
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/resourcify/parent_relationship.rb', line 19 def self.included(base) base.class_eval do class_inheritable_accessor :relationship_collections self.relationship_collections = {} def self.access_by(relationship, = {}) [:collection] ||= Extlib::Inflection.tableize(model_name_parts.last).to_sym relationship_collections[relationship] = [:collection] end end end |
Instance Method Details
#collection(query = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/resourcify/parent_relationship.rb', line 42 def collection(query = {}) if parent_relationship? if association_name = relationship_collections[parent_relationship.name] parent.send(association_name, query) else raise(ArgumentError.new( "Parent relationship #{parent_relationship.name} found, but not marked as accessible. Specify `access_by :#{parent_relationship.name}` for this controller." )) end end end |
#owner ⇒ Object
34 35 36 |
# File 'lib/resourcify/parent_relationship.rb', line 34 def owner collection || model end |
#parent ⇒ Object
38 39 40 |
# File 'lib/resourcify/parent_relationship.rb', line 38 def parent @parent ||= parent_relationship.parent_model.get!(params[parent_key]) if parent_relationship? end |
#parent_key ⇒ Object
67 68 69 |
# File 'lib/resourcify/parent_relationship.rb', line 67 def parent_key @parent_key ||= model.parent_keys.detect { |parent_key| params[parent_key] } end |
#parent_relationship ⇒ Object
63 64 65 |
# File 'lib/resourcify/parent_relationship.rb', line 63 def parent_relationship @parent_relationship ||= model.parent_relationships[parent_key] end |
#parent_relationship? ⇒ Boolean
59 60 61 |
# File 'lib/resourcify/parent_relationship.rb', line 59 def parent_relationship? !parent_relationship.nil? end |
#relationship_collections ⇒ Object
55 56 57 |
# File 'lib/resourcify/parent_relationship.rb', line 55 def relationship_collections self.class.relationship_collections end |