Module: Intermodal::Models::ResourceLinking::ClassMethods

Includes:
HasParentResource::ClassMethods
Defined in:
lib/intermodal/concerns/models/resource_linking.rb

Instance Method Summary collapse

Methods included from HasParentResource::ClassMethods

#parent_resource

Methods included from Accountability::Get

#get

Instance Method Details

#append(parent_id, resource_ids, options = {}) ⇒ Object

.append

- adds new resources into the list
- does not add redundent resources


102
103
104
105
106
107
108
109
110
# File 'lib/intermodal/concerns/models/resource_linking.rb', line 102

def append(parent_id, resource_ids, options = {})
  transaction do
    parent = parent_model.get(parent_id, options.except(:parent, :parent_id))
    return nil unless parent

    resource_ids = resource_ids - by_parent_id(parent_id).by_target_id(resource_ids).to_target_ids
    parent.send("#{target_resource_name.to_s.pluralize}") << target_model.get(:all, options).where(:id => resource_ids)
  end
end

#build_get_query(_query, opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/intermodal/concerns/models/resource_linking.rb', line 72

def build_get_query(_query, opts)
  _query = super(_query, opts)
  #_query.build_arel
  #_query = _query.by_target_account(opts[:account]) if opts[:account]
  #_query = _query.by_target_account_id(opts[:account_id]) if opts[:account_id]
  # HACK:
  # Workaround for Arel bug
  #_query = _query._by_target_account_id(opts[:account_id]) if opts[:account_id]
  return _query
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/intermodal/concerns/models/resource_linking.rb', line 14

def links_resource(_parent, options = {})
  raise "Must supply :to" unless options[:to]

  _target_resource = options[:to]
  _target_resource_class_name = options[:class_name]
  _target_table_name = options[:target_table_name] || _target_resource.to_s.pluralize
  _parent_resource_class_name = options[:parent_class_name]
  _parent_table_name = options[:parent_table_name] || _parent.to_s.pluralize

  instance_eval do
    parent_resource(_parent, :class_name => _parent_resource_class_name)

    # Associations
    if _target_resource_class_name
      belongs_to _target_resource, :class_name => _target_resource_class_name
    else
      belongs_to _target_resource
    end

    # Scopes
    scope "by_#{_target_resource}_id", lambda { |_id| where("#{target_resource_name}_id" => _id) }
    scope :by_target_id, lambda { |_id| send("by_#{target_resource_name}_id", _id) }
    #scope :by_target_account_id, lambda { |a_id| 
    #    joins(_target_resource).
    #    where(_target_table_name => { :account_id => a_id })}
    #scope :by_target_account, lambda { |a| by_target_account_id(a.id) }
    #scope :_by_target_account_id, lambda { |a_id| where(_target_table_name => { :account_id => a_id})}

    # Transformations
    def self.to_target_ids
      select("#{target_resource_name}_id").map(&"#{target_resource_name}_id".to_sym)
    end

    # Links parent resource and target resource
    class_attribute :target_resource_name, :target_resource_class_name, :parent_resource_name, :parent_resource_class_name

    def self.parent_model
      (parent_resource_class_name || parent_resource_name.to_s.camelize).constantize
    end

    def self.target_model
      (target_resource_class_name || target_resource_name.to_s.camelize).constantize
    end

    singleton_class.instance_eval do
      define_method "to_#{_target_resource}_ids" do
        to_target_ids
      end
    end
  end

  # Set class inheritable variables
  self.target_resource_name = _target_resource
  self.target_resource_class_name = _target_resource_class_name
  self.parent_resource_name = _parent
  self.parent_resource_class_name = _parent_resource_class_name
end

#remove(parent_id, resource_ids, options = {}) ⇒ Object

.remove

- removes specified lists


114
115
116
117
118
119
120
121
122
# File 'lib/intermodal/concerns/models/resource_linking.rb', line 114

def remove(parent_id, resource_ids, options = {})
  transaction do
    parent = parent_model.get(parent_id, options.except(:parent, :parent_id))
    return nil unless parent

    by_parent_id(parent_id).by_target_id(resource_ids).delete_all
    by_parent_id(parent_id).to_target_ids
  end
end

#replace(parent_id, resource_ids, options = {}) ⇒ Object

.replace

- replaces existing resources with new resource ids
- existing resources gets deleted


88
89
90
91
92
93
94
95
96
97
# File 'lib/intermodal/concerns/models/resource_linking.rb', line 88

def replace(parent_id, resource_ids, options = {})
  transaction do
    parent = parent_model.get(parent_id, options.except(:parent, :parent_id))
    return nil unless parent

    targets = target_model.get(:all, options).where(:id => resource_ids)
    parent.send("#{target_resource_name.to_s.pluralize}=", targets)
    by_parent_id(parent_id).to_target_ids
  end
end