Module: ActsAsDataOwner

Defined in:
lib/acts_as_data_owner/version.rb,
lib/acts_as_data_owner/active_record.rb

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

VERSION =
'0.0.7'

Instance Method Summary collapse

Instance Method Details

#acts_as_data_owner(*args) ⇒ Object Also known as: data_owner



7
8
9
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
# File 'lib/acts_as_data_owner/active_record.rb', line 7

def acts_as_data_owner(*args)

  options = args.extract_options!
  fields = [:creator, :owner, :updater]
  fields += [options[:include]].flatten if options[:include]
  fields -= [options[:exclude]].flatten if options[:exclude]

  attr_protected fields.map { |f| "#{f}_id" }

  (fields-[:company]).each do |f|
    belongs_to f, :class_name => "Person", :foreign_key => "#{f}_id"
    before_create :"set_#{f}"
  end

  if fields.include?(:company)
    belongs_to :company
    before_create :set_company
  end


  scope :current, lambda {
    conditions = {}
    #Filter by User Ids Who has access to Products
    if !Person.current.is_admin?

      conditions["#{self.table_name}.owner_id"] = ([Person.current.id] + Person.manage_people_ids).compact if fields.include?(:owner)

      if fields.include?(:company)
        company_ids = ([(Person.current.current_company ? Person.current.current_company.id : nil)] + Person.manage_company_ids).compact
        conditions["#{self.table_name}.company_id"] = company_ids if company_ids.any?
      end
    end
    where(conditions)
  }

  scope :personal, lambda {
    conditions = {}
    #Filter by User Ids Who has access to Products
      conditions["#{self.table_name}.owner_id"] = ([Person.current.id] + Person.manage_people_ids).compact if fields.include?(:owner)

      if fields.include?(:company)
        company_ids = ([(Person.current.current_company ? Person.current.current_company.id : nil)] + Person.manage_company_ids).compact
        conditions["#{self.table_name}.company_id"] = company_ids if company_ids.any?
      end

    where(conditions)
  }


  #has_paper_trail(:meta => {:person_id => Proc.new { |p| (p.updater_id || p.creator_id) }})

  include InstanceMethods
end