acts_as_data_owner 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module ActsAsDataOwner
3
+
4
+ def acts_as_data_owner(*args)
5
+
6
+ options = args.extract_options!
7
+ fields = [:creator, :owner, :updater]
8
+ fields += [options[:include]].flatten if options[:include]
9
+ fields -= [options[:exclude]].flatten if options[:exclude]
10
+
11
+ attr_protected fields.map { |f| "#{f}_id" }
12
+
13
+ (fields-[:company]).each do |f|
14
+ belongs_to f, :class_name => "Person", :foreign_key => "#{f}_id"
15
+ end
16
+
17
+ if fields.include?(:company)
18
+ belongs_to :company
19
+ before_create :set_company
20
+ end
21
+ before_create :set_creator_and_owner
22
+ before_update :set_updater
23
+
24
+ scope :current, lambda {
25
+ #Filter by User Ids Who has access to Products
26
+ if !Person.current.is_admin?
27
+ conditions = {}
28
+ conditions["#{self.table_name}.creator_id"] = ([Person.current.id] + Person.manage_people_ids).compact if fields.include?(:creator)
29
+ conditions["#{self.table_name}.company_id"] = ([(Person.current.current_company ? Person.current.current_company.id : nil)] + Person.manage_company_ids).compact if fields.include?(:company)
30
+ where(conditions)
31
+ end
32
+ }
33
+
34
+
35
+ #has_paper_trail(:meta => {:person_id => Proc.new { |p| (p.updater_id || p.creator_id) }})
36
+
37
+ include InstanceMethods
38
+ end
39
+
40
+ module InstanceMethods
41
+ private
42
+
43
+ def set_company
44
+ self.company = Person.current.company
45
+ end
46
+
47
+ def set_creator_and_owner
48
+ self.creator = Person.current
49
+ self.owner = Person.current
50
+ end
51
+
52
+ def set_updater
53
+ self.updater = Person.current
54
+ end
55
+
56
+ end
57
+ end
58
+
59
+
60
+
61
+
62
+
@@ -0,0 +1,24 @@
1
+ module ActiveRecord
2
+ class Migration
3
+
4
+ def acts_as_data_owner_for(table_name, *args)
5
+ options = args.extract_options!
6
+ fields = [:creator, :owner, :updater]
7
+ fields += [options[:include]].flatten if options[:include]
8
+ fields -= [options[:exclude]].flatten if options[:exclude]
9
+
10
+ #Add fields to table
11
+ change_table table_name do |t|
12
+ fields.each do |f|
13
+ t.references(f)
14
+ end
15
+ end
16
+
17
+ #Add indexes
18
+ fields.each do |f|
19
+ add_index table_name, "#{f}_id"
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module ActsAsDataOwner
3
+ VERSION = '0.0.2'
4
+ end
@@ -0,0 +1,7 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'acts_as_data_owner/active_record'
3
+ require 'acts_as_data_owner/migration'
4
+
5
+
6
+
7
+ ActiveRecord::Base.extend ActsAsDataOwner
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_data_owner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Belorusov Dmitriy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: The Access Data framework for Ruby on Rails.
31
+ email:
32
+ - Develby@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/acts_as_data_owner/active_record.rb
38
+ - lib/acts_as_data_owner/version.rb
39
+ - lib/acts_as_data_owner/migration.rb
40
+ - lib/acts_as_data_owner.rb
41
+ homepage: http://worldalias.com
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.19
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: The Access Data framework for Ruby on Rails.
65
+ test_files: []