acts_as_owner 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril Wack
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ MIT-LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ VERSION.yml
5
+ init.rb
6
+ lib/acts_as_owner.rb
7
+ Manifest
@@ -0,0 +1,36 @@
1
+ = Acts as owner
2
+
3
+ Provides to a user the ability to self-query about the possession of a given resource or a given account.
4
+
5
+ Any resources belonging to an other resource which is owned by the user are considered such as owned by this user too.
6
+
7
+ == Install
8
+
9
+ Install the gem (recommended):
10
+
11
+ sudo gem install acts_as_owner
12
+
13
+ Or you can install this as a plugin:
14
+
15
+ script/plugin install git://github.com/cyril/acts_as_owner.git
16
+
17
+ == Example
18
+
19
+ class User < ActiveRecord::Base
20
+ acts_as_owner :categories, :articles, :comments
21
+ end
22
+
23
+ # Considering this article:
24
+ article = current_user.articles.first
25
+
26
+ # We can see that:
27
+ current_user.owns? article # => true
28
+ current_user.owns? article.user # => true
29
+
30
+ # Now, check if its first comment is ours:
31
+ current_user.owns? article.comments.first.user # => false
32
+
33
+ # But even if it is not, this comment is a child of our article, so:
34
+ current_user.owns? article.comments.first # => true
35
+
36
+ Copyright (c) 2009 Cyril Wack, released under the MIT license
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('acts_as_owner', '1.2.0') do |p|
6
+ p.description = "Simple Rails plugin that allows to operate freely on objects which belong to us."
7
+ p.url = "http://github.com/cyril/acts_as_owner"
8
+ p.author = "Cyril Wack"
9
+ p.email = "cyril.wack@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 2
4
+ :patch: 0
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{acts_as_owner}
5
+ s.version = "1.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Cyril Wack"]
9
+ s.cert_chain = ["/Users/cyril/gem-public_cert.pem"]
10
+ s.date = %q{2010-04-12}
11
+ s.description = %q{Simple Rails plugin that allows to operate freely on objects which belong to us.}
12
+ s.email = %q{cyril.wack@gmail.com}
13
+ s.extra_rdoc_files = ["README.rdoc", "lib/acts_as_owner.rb"]
14
+ s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "VERSION.yml", "init.rb", "lib/acts_as_owner.rb", "Manifest", "acts_as_owner.gemspec"]
15
+ s.homepage = %q{http://github.com/cyril/acts_as_owner}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_owner", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{acts_as_owner}
19
+ s.rubygems_version = %q{1.3.6}
20
+ s.signing_key = %q{/Users/cyril/gem-private_key.pem}
21
+ s.summary = %q{Simple Rails plugin that allows to operate freely on objects which belong to us.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_owner'
@@ -0,0 +1,74 @@
1
+ require 'active_record/base'
2
+
3
+ module ActsAsOwner
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ # Configuration resources are: all resources belonging to the current model.
10
+ def acts_as_owner(*resources)
11
+ resources.each { |resource| has_many resource.to_s.tableize.to_sym, :dependent => :destroy }
12
+
13
+ class_eval <<-EOV
14
+ include ActsAsOwner::InstanceMethods
15
+
16
+ def self.owns_many
17
+ reflections.select { |name, reflection| reflection.macro == :has_many }.collect { |table| table[0].to_s.singularize }
18
+ end
19
+
20
+ def self.owns_one
21
+ reflections.select { |name, reflection| reflection.macro == :has_one }.collect { |table| table[0].to_s.singularize }
22
+ end
23
+ EOV
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ # Returns true if the user owns the object which can be a account or a resource. Otherwise returns false.
29
+ def owns?(object = nil)
30
+ return false if object.nil?
31
+ object.is_a?(self.class) ? self.owns_this_account?(object) : self.owns_this_resource?(object)
32
+ end
33
+
34
+ protected
35
+
36
+ # Returns true if the user owns the account, otherwise returns false.
37
+ def owns_this_account?(account)
38
+ self.id == account.id
39
+ end
40
+
41
+ # Returns true if the user owns the resource, otherwise returns false.
42
+ def owns_this_resource?(resource)
43
+ (has_many?(resource) || has_one?(resource)) ||
44
+ parents_of(resource).collect { |parent| owns_this_resource?(parent) }.select { |result| result == true }.uniq.pop == true
45
+ end
46
+
47
+ # Has many association
48
+ def has_many?(resource)
49
+ self.respond_to?(resource.class.name.tableize) && self.send(resource.class.name.tableize).include?(resource)
50
+ end
51
+
52
+ # Has one association
53
+ def has_one?(resource)
54
+ self.respond_to?(resource.class.name.tableize.singularize) && self.send(resource.class.name.tableize.singularize) == resource
55
+ end
56
+
57
+ # Returns an array of resources that are:
58
+ # * directly superior to the resource passed as parameter, according the hierarchical tree structure;
59
+ # * potentially owned by the current user, through the hierarchical relationship.
60
+ def parents_of(resource)
61
+ potential_owners_of(resource).collect { |method| resource.send(method) }
62
+ end
63
+
64
+ # Returns an array containing symbols common to children
65
+ # which belongs to the current user and to parents which has many or has one given resource.
66
+ def potential_owners_of(resource)
67
+ (self.class.owns_many | self.class.owns_one) & resource.class.reflections.select do |name, reflection|
68
+ reflection.macro == :belongs_to
69
+ end.collect { |table| table[0].to_s }
70
+ end
71
+ end
72
+ end
73
+
74
+ ActiveRecord::Base.class_eval { include ActsAsOwner }
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_owner
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 2
8
+ - 0
9
+ version: 1.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Cyril Wack
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMRMwEQYDVQQDDApjeXJp
19
+ bC53YWNrMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZFgNj
20
+ b20wHhcNMTAwNDExMjI1OTI4WhcNMTEwNDExMjI1OTI4WjBBMRMwEQYDVQQDDApj
21
+ eXJpbC53YWNrMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZ
22
+ FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDGJFqPSXGYdS6t
23
+ t+kXyFuLg7uIbQHToLhdbfpu5j7dl65EWAspRI37ZE/FIFosmwQ0DAGiJ35gVX7K
24
+ 5/rj745EUW9vijemlNHZjTY40AQAP2avlcMd6DnA7pl/x8dxC9G2dW/IS0nmjH0E
25
+ +X7X0BZ8WIY7PBvLsq5ptpGaoaxpmqRjJDANolnODwyBjFWtUqpvOGeUWL24orZ3
26
+ xwcW6d1vl8hraZ3UUJtIVXFg85lHclyrP33DYxj5sstgRwovaCPrvUQ6ZZ+hX/iJ
27
+ MYaEFZsw74WVD4RLHl1bEz2RQGDgSwfFnOnrQ1gi2SaeqUN7uFThAEbbyiJK+rNL
28
+ xapWOFs9AgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
29
+ BBSsO/YPzLP2/ZtBKQfWveZNRK3uKDANBgkqhkiG9w0BAQUFAAOCAQEAGaGpPlEc
30
+ Z8A7Jtfws1tpdLOlQrQQXfIgBrPvfjO18MxT2BVgnusYcMuJgrY1skbH6RDxhdia
31
+ EetICD0kvyGnbK+dHdhRwvmmiqc7ZOaiFb3RNLcW6jduxafH4zgKUeg23KpfJYy3
32
+ MOqVgHckM1hMZTWz7nmrXJBAjj/48jFOPrwtTed8kd6KpIjUz4e2oTwT+JIVnryF
33
+ sYFesvR4DywbXL88T29gq5biCHsAgbK89DW5DNx1Yg1HNLxCdJurJFrcQQS3XQco
34
+ h2svBTlG7Yg1wLZAGkVx4RSkrFujrxpgLsz5bfmdnbiEgKcF9njIOdVO4P4vwyoS
35
+ G4VzQZjAGxprTw==
36
+ -----END CERTIFICATE-----
37
+
38
+ date: 2010-04-12 00:00:00 +02:00
39
+ default_executable:
40
+ dependencies: []
41
+
42
+ description: Simple Rails plugin that allows to operate freely on objects which belong to us.
43
+ email: cyril.wack@gmail.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README.rdoc
50
+ - lib/acts_as_owner.rb
51
+ files:
52
+ - MIT-LICENSE
53
+ - README.rdoc
54
+ - Rakefile
55
+ - VERSION.yml
56
+ - init.rb
57
+ - lib/acts_as_owner.rb
58
+ - Manifest
59
+ - acts_as_owner.gemspec
60
+ has_rdoc: true
61
+ homepage: http://github.com/cyril/acts_as_owner
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --line-numbers
67
+ - --inline-source
68
+ - --title
69
+ - Acts_as_owner
70
+ - --main
71
+ - README.rdoc
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 1
87
+ - 2
88
+ version: "1.2"
89
+ requirements: []
90
+
91
+ rubyforge_project: acts_as_owner
92
+ rubygems_version: 1.3.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Simple Rails plugin that allows to operate freely on objects which belong to us.
96
+ test_files: []
97
+
Binary file