acts_as_owner 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +11 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +59 -16
- data/Rakefile +11 -11
- data/VERSION.yml +2 -2
- data/acts_as_owner.gemspec +13 -27
- data/init.rb +1 -1
- data/lib/acts_as_owner.rb +49 -54
- data/test/owner_test.rb +121 -0
- data/test/test_helper.rb +4 -0
- metadata +50 -76
- data/Manifest +0 -7
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.2@acts_as_owner
|
data/Gemfile
ADDED
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,36 +1,79 @@
|
|
1
1
|
= Acts as owner
|
2
2
|
|
3
|
-
|
3
|
+
Acts as owner is a plugin for Ruby on Rails that provides to an owner object the
|
4
|
+
ability to self-query about the possession of an ownable object.
|
4
5
|
|
5
|
-
|
6
|
+
Ownable objects can be any objects belonging to the owner object and any objects
|
7
|
+
belonging to an ownable object.
|
6
8
|
|
7
|
-
|
9
|
+
Any ownable child, which belongs to an owned ownable parent, is also owned by
|
10
|
+
the owner.
|
8
11
|
|
9
|
-
|
12
|
+
== Philosophy
|
10
13
|
|
11
|
-
|
14
|
+
General library that does only one thing, without any feature.
|
12
15
|
|
13
|
-
|
16
|
+
== Installation
|
14
17
|
|
15
|
-
|
18
|
+
Include the gem in your <tt>Gemfile</tt>:
|
19
|
+
|
20
|
+
gem 'acts_as_owner'
|
21
|
+
|
22
|
+
And run the +bundle+ command. Or as a plugin:
|
23
|
+
|
24
|
+
rails plugin install git://github.com/cyril/acts_as_owner.git
|
25
|
+
|
26
|
+
== Getting started
|
27
|
+
|
28
|
+
=== Configuring models
|
29
|
+
|
30
|
+
Owner models just have to be declared thanks to <tt>acts_as_owner</tt>, with
|
31
|
+
each ownable object passed through a block (using <tt>owns_one</tt> or
|
32
|
+
<tt>owns_many</tt> method).
|
16
33
|
|
17
34
|
== Example
|
18
35
|
|
19
36
|
class User < ActiveRecord::Base
|
20
|
-
acts_as_owner
|
37
|
+
acts_as_owner do |u|
|
38
|
+
u.owns_many :articles
|
39
|
+
u.owns_many :comments
|
40
|
+
end
|
41
|
+
|
42
|
+
with_options :dependent => :destroy do |opts|
|
43
|
+
opts.has_many :articles
|
44
|
+
opts.has_many :comments
|
45
|
+
end
|
21
46
|
end
|
22
47
|
|
23
|
-
|
48
|
+
class Article < ActiveRecord::Base
|
49
|
+
belongs_to :user
|
50
|
+
has_many :comments, :dependent => :destroy
|
51
|
+
end
|
52
|
+
|
53
|
+
class Comment < ActiveRecord::Base
|
54
|
+
belongs_to :article
|
55
|
+
belongs_to :user
|
56
|
+
end
|
57
|
+
|
58
|
+
# An article should be ownable by a user, so:
|
59
|
+
User.could_own_an?(:article) # => true
|
60
|
+
|
61
|
+
# Considering this one:
|
24
62
|
article = current_user.articles.first
|
25
63
|
|
26
64
|
# We can see that:
|
27
|
-
current_user.owns? article
|
28
|
-
|
65
|
+
current_user.owns? article # => true
|
66
|
+
|
67
|
+
# Now, considering its first comment:
|
68
|
+
comment = article.comments.first
|
69
|
+
|
70
|
+
# Just like article, we can check that:
|
71
|
+
User.could_own_an? :comment # => true
|
29
72
|
|
30
|
-
#
|
31
|
-
current_user
|
73
|
+
# Let's see if the current user is the author:
|
74
|
+
current_user == comment.user # => false
|
32
75
|
|
33
|
-
#
|
34
|
-
current_user.owns?
|
76
|
+
# Never mind. It belongs to his article so that's also his one:
|
77
|
+
current_user.owns? comment # => true
|
35
78
|
|
36
|
-
Copyright (c) 2009 Cyril Wack, released under the MIT license
|
79
|
+
Copyright (c) 2009-2011 Cyril Wack, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
require 'echoe'
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.libs << 'test'
|
9
|
+
t.test_files = FileList["test/**/*_{helper,test}.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :test
|
data/VERSION.yml
CHANGED
data/acts_as_owner.gemspec
CHANGED
@@ -1,32 +1,18 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
1
|
Gem::Specification.new do |s|
|
4
|
-
s.name
|
5
|
-
s.version
|
6
|
-
|
7
|
-
s.
|
8
|
-
s.
|
9
|
-
s.
|
10
|
-
s.
|
2
|
+
s.name = "acts_as_owner"
|
3
|
+
s.version = Psych.load_file("VERSION.yml").values.join('.')
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Cyril Wack"]
|
6
|
+
s.email = ["cyril@gosu.fr"]
|
7
|
+
s.homepage = "http://github.com/cyril/acts_as_owner"
|
8
|
+
s.summary = %q{Simple ownership solution for Rails.}
|
11
9
|
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
10
|
|
23
|
-
|
24
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
-
s.specification_version = 3
|
11
|
+
s.rubyforge_project = "acts_as_owner"
|
26
12
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
13
|
+
s.add_runtime_dependency "railties", ">= 3.0.0"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = ["lib"]
|
32
18
|
end
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'acts_as_owner'
|
1
|
+
require 'acts_as_owner'
|
data/lib/acts_as_owner.rb
CHANGED
@@ -1,74 +1,69 @@
|
|
1
|
-
require 'active_record/base'
|
2
|
-
|
3
1
|
module ActsAsOwner
|
4
|
-
def
|
5
|
-
|
2
|
+
def owns?(obj)
|
3
|
+
can_own_this?(obj) if self.class.could_own_a?(obj.class.name.underscore)
|
6
4
|
end
|
7
5
|
|
8
|
-
|
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 }
|
6
|
+
protected
|
12
7
|
|
13
|
-
|
14
|
-
|
8
|
+
def can_own_this?(obj, depth = 0)
|
9
|
+
association_id = obj.class.name.tableize.singularize.to_sym
|
10
|
+
association_params = self.class.
|
11
|
+
read_inheritable_attribute(:ownable_ids)[association_id]
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
unless depth > association_params[:max_depth]
|
14
|
+
owner_id = association_params[:owner_id]
|
15
|
+
owned = obj.respond_to?(owner_id) && obj.send(owner_id) == self
|
19
16
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
EOV
|
17
|
+
owned || ownable_ancestors(obj).any? do |name|
|
18
|
+
can_own_this?(name, depth.next)
|
19
|
+
end
|
24
20
|
end
|
25
21
|
end
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
return false if object.nil?
|
31
|
-
object.is_a?(self.class) ? self.owns_this_account?(object) : self.owns_this_resource?(object)
|
23
|
+
def ownable_ancestors(child)
|
24
|
+
ancestors = child.class.reflections.select do |name, reflection|
|
25
|
+
reflection.macro == :belongs_to
|
32
26
|
end
|
33
27
|
|
34
|
-
|
28
|
+
ancestors.map {|table| table.first }.
|
29
|
+
select {|name| self.class.could_own_a?(name) }.
|
30
|
+
map {|name| child.send(name) }.compact
|
31
|
+
end
|
32
|
+
end
|
35
33
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
class ActiveRecord::Base
|
35
|
+
def self.acts_as_owner(options = {}, &block)
|
36
|
+
configuration = {
|
37
|
+
:max_depth => 50,
|
38
|
+
:owner_id => name.tableize.singularize.to_sym }
|
39
|
+
configuration.update(options) if options.is_a?(Hash)
|
40
40
|
|
41
|
-
|
42
|
-
|
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
|
41
|
+
write_inheritable_hash(:ownership_options, configuration)
|
42
|
+
write_inheritable_hash(:ownable_ids, {})
|
46
43
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
class << self
|
45
|
+
def owns_many(association_id, options = {})
|
46
|
+
read_inheritable_attribute(:ownable_ids).update({
|
47
|
+
association_id.to_s.singularize.to_sym =>
|
48
|
+
read_inheritable_attribute(:ownership_options).merge(options) })
|
49
|
+
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
def owns_one(association_id, options = {})
|
52
|
+
read_inheritable_attribute(:ownable_ids).update({
|
53
|
+
association_id.to_sym =>
|
54
|
+
read_inheritable_attribute(:ownership_options).merge(options) })
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
potential_owners_of(resource).collect { |method| resource.send(method) }
|
62
|
-
end
|
57
|
+
def could_own_a?(association_id)
|
58
|
+
read_inheritable_attribute(:ownable_ids).keys.
|
59
|
+
include?(association_id.to_sym)
|
60
|
+
end
|
63
61
|
|
64
|
-
|
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 }
|
62
|
+
alias_method :could_own_an?, :could_own_a?
|
70
63
|
end
|
64
|
+
|
65
|
+
instance_eval &block
|
66
|
+
|
67
|
+
include ActsAsOwner
|
71
68
|
end
|
72
69
|
end
|
73
|
-
|
74
|
-
ActiveRecord::Base.class_eval { include ActsAsOwner }
|
data/test/owner_test.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
:adapter => 'sqlite3', :database => ':memory:')
|
5
|
+
|
6
|
+
def setup_db
|
7
|
+
ActiveRecord::Schema.define(:version => 1) do
|
8
|
+
create_table :users do |t|
|
9
|
+
t.string :login
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :blogs do |t|
|
13
|
+
t.references :user, :null => false
|
14
|
+
t.string :title
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :categories do |t|
|
18
|
+
t.references :blog, :null => false
|
19
|
+
t.string :title
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :articles do |t|
|
23
|
+
t.references :publishable, :polymorphic => true, :null => false
|
24
|
+
t.references :user
|
25
|
+
t.string :title
|
26
|
+
t.text :content
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table :comments do |t|
|
30
|
+
t.references :article, :null => false
|
31
|
+
t.references :user
|
32
|
+
t.text :content
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown_db
|
38
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
39
|
+
ActiveRecord::Base.connection.drop_table(table)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class User < ActiveRecord::Base
|
44
|
+
acts_as_owner(:verbose => true) do |a|
|
45
|
+
a.owns_many :categories
|
46
|
+
a.owns_many :articles
|
47
|
+
a.owns_many :comments
|
48
|
+
a.owns_many :publishables
|
49
|
+
a.owns_one :blog
|
50
|
+
end
|
51
|
+
|
52
|
+
has_one :blog, :dependent => :destroy
|
53
|
+
has_many :articles, :dependent => :destroy
|
54
|
+
has_many :comments, :dependent => :destroy
|
55
|
+
end
|
56
|
+
|
57
|
+
class Blog < ActiveRecord::Base
|
58
|
+
belongs_to :user
|
59
|
+
has_many :articles, :as => :publishable, :dependent => :destroy
|
60
|
+
has_many :categories, :dependent => :destroy
|
61
|
+
end
|
62
|
+
|
63
|
+
class Category < ActiveRecord::Base
|
64
|
+
belongs_to :blog
|
65
|
+
has_many :articles, :as => :publishable, :dependent => :destroy
|
66
|
+
end
|
67
|
+
|
68
|
+
class Article < ActiveRecord::Base
|
69
|
+
belongs_to :publishable, :polymorphic => true
|
70
|
+
belongs_to :user
|
71
|
+
has_many :comments, :dependent => :destroy
|
72
|
+
end
|
73
|
+
|
74
|
+
class Comment < ActiveRecord::Base
|
75
|
+
belongs_to :article
|
76
|
+
belongs_to :user
|
77
|
+
end
|
78
|
+
|
79
|
+
class OwnerTest < MiniTest::Unit::TestCase
|
80
|
+
def setup
|
81
|
+
setup_db
|
82
|
+
|
83
|
+
@admin = User.create! :login => 'admin'
|
84
|
+
@bob = User.create :login => 'bob'
|
85
|
+
|
86
|
+
@blog = @admin.create_blog :title => 'my_blog'
|
87
|
+
@category = @blog.categories.create! :title => 'main'
|
88
|
+
@article0 = @category.articles.create! :title => 'hello, world',
|
89
|
+
:user => @admin
|
90
|
+
@article1 = @blog.articles.create! :title => 'hello, all',
|
91
|
+
:user => @bob
|
92
|
+
@comment0 = @article0.comments.create! :content => 'foo'
|
93
|
+
@comment1 = @article0.comments.create! :content => 'bar',
|
94
|
+
:user => @bob
|
95
|
+
end
|
96
|
+
|
97
|
+
def teardown
|
98
|
+
teardown_db
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_the_ownability
|
102
|
+
refute User.could_own_a?(:tank)
|
103
|
+
refute User.could_own_a?("dinosaur")
|
104
|
+
refute User.could_own_a?(42.to_s)
|
105
|
+
assert User.could_own_a?(:blog)
|
106
|
+
assert User.could_own_an?(:article)
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_the_ownership
|
110
|
+
assert @admin.owns?(@blog)
|
111
|
+
refute @bob.owns?(@blog)
|
112
|
+
assert @admin.owns?(@article0)
|
113
|
+
refute @bob.owns?(@article0)
|
114
|
+
assert @admin.owns?(@article1)
|
115
|
+
assert @bob.owns?(@article1)
|
116
|
+
assert @admin.owns?(@comment0)
|
117
|
+
refute @bob.owns?(@comment0)
|
118
|
+
assert @bob.owns?(@comment1)
|
119
|
+
assert @admin.owns?(@comment1)
|
120
|
+
end
|
121
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,97 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_owner
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 2
|
8
|
-
- 0
|
9
|
-
version: 1.2.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Cyril Wack
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
|
-
cert_chain:
|
16
|
-
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-04-22 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: &2153341400 !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: *2153341400
|
25
|
+
description: Simple Rails plugin that allows to operate freely on objects which belong
|
26
|
+
to us.
|
27
|
+
email:
|
28
|
+
- cyril@gosu.fr
|
44
29
|
executables: []
|
45
|
-
|
46
30
|
extensions: []
|
47
|
-
|
48
|
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rvmrc
|
35
|
+
- Gemfile
|
52
36
|
- MIT-LICENSE
|
53
37
|
- README.rdoc
|
54
38
|
- Rakefile
|
55
39
|
- VERSION.yml
|
40
|
+
- acts_as_owner.gemspec
|
56
41
|
- init.rb
|
57
42
|
- lib/acts_as_owner.rb
|
58
|
-
-
|
59
|
-
-
|
60
|
-
has_rdoc: true
|
43
|
+
- test/owner_test.rb
|
44
|
+
- test/test_helper.rb
|
61
45
|
homepage: http://github.com/cyril/acts_as_owner
|
62
46
|
licenses: []
|
63
|
-
|
64
47
|
post_install_message:
|
65
|
-
rdoc_options:
|
66
|
-
|
67
|
-
- --inline-source
|
68
|
-
- --title
|
69
|
-
- Acts_as_owner
|
70
|
-
- --main
|
71
|
-
- README.rdoc
|
72
|
-
require_paths:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
73
50
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
- 1
|
87
|
-
- 2
|
88
|
-
version: "1.2"
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
89
63
|
requirements: []
|
90
|
-
|
91
64
|
rubyforge_project: acts_as_owner
|
92
|
-
rubygems_version: 1.
|
65
|
+
rubygems_version: 1.7.2
|
93
66
|
signing_key:
|
94
67
|
specification_version: 3
|
95
|
-
summary: Simple
|
96
|
-
test_files:
|
97
|
-
|
68
|
+
summary: Simple ownership solution for Rails.
|
69
|
+
test_files:
|
70
|
+
- test/owner_test.rb
|
71
|
+
- test/test_helper.rb
|
data/Manifest
DELETED
data.tar.gz.sig
DELETED
Binary file
|
metadata.gz.sig
DELETED
Binary file
|