- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 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
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
def entities(model, params = {})
@entities ||= {}
@entities[model] ||= []
if @entities[model].blank? or parameters_changed?(model, params)
@entities[model] = []
include = params[:include] || nil
group = params[:group] || nil
order = params[:order] || nil
page = params[:page] || nil
entity_ids = []
model_role = nil
model_class = model.to_s.classify.constantize
if self.has_role_for?(model_class)
self.roles_for(model_class).uniq.each do |role|
if role.authorizable_id.blank?
raise "Authorization problem! Found more than one #{model_class} model permission!" unless model_role.blank?
model_role = role.name
next
end
if !role.authorizable_id.blank? && MerchantRole::MERCHANT_ROLES.include?(role.name)
entity_ids << role.authorizable.id
end
end
if entity_ids.blank? and MerchantRole::MERCHANT_ROLES.include?(model_role)
#WARNING! RECURSION! EVIL!
if MerchantRole.has_entity_parent?(model)
parent_model = MerchantRole.entity_parent(model)
parent_entities = self.entities(parent_model)
unless parent_entities.blank?
_params = {
:conditions => conditions_and_parameters(params, nil, ["`#{model.to_s}`.`#{parent_model.to_s.singularize}_id` IN (?)", parent_entities.map(&:id)]),
:include => include,
:order => order,
:group => group
}
_params.merge!(:page => page) if params.keys.include?(:page)
if block_given?
(self.is_reseller? ? self.reseller.send(model) : model_class).each(_params) do |o|
yield o
end
else
@entities[model] = (self.is_reseller? ? self.reseller.send(model) : model_class).send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
end
end
else
_params = {
:conditions => conditions_and_parameters(params, nil),
:include => include,
:order => order,
:group => group
}
_params.merge!(:page => page) if params.keys.include?(:page)
if block_given? #Find only merchants associated with the reseller or all merchants if we are no reseller.
(self.is_reseller? ? self.reseller.send(model) : model_class).each(_params) do |o|
yield o
end
else
@entities[model] = (self.is_reseller? ? self.reseller.send(model) : model_class).send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
end
end
else
_params = { :conditions => conditions_and_parameters(params, nil, ["`#{model.to_s}`.`id` IN (?)", entity_ids]),
:include => include,
:order => order,
:group => group }
_params.merge!(:page => page) if params.keys.include?(:page)
if block_given?
model_class.each(_params) do |o|
yield o
end
else
@entities[model] = model_class.send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
end
end
end
end
entities[@model]
end