ownable 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,18 +4,21 @@ Ownable gem helps you verify the "ownership" of a associated models in Rails 3.
4
4
 
5
5
  The term "ownership" in the contact of Ownable is the association between two models, usually a User Model (though any model can do) and some other Model.
6
6
 
7
- Ownable becomes incresa
7
+ Ownable exposes an instance method on ActiveRecord objects that declare themselves to be acts_as_ownable:
8
8
 
9
- For example:
9
+ owned_by? (and its alias belongs_to?)
10
+
11
+ For example, assume the associations:
10
12
 
11
13
  User has_many Orders has_one Invoice has_many Payments
12
14
 
15
+ Determine if a User "owns" a particular Payment object
16
+
13
17
  Rather than manually defining and then checking the association between User and Payment like:
14
- - @payment.invoice.order.user == current_user # => true/false
18
+ @payment.invoice.order.user == current_user # => true/false
15
19
 
16
20
  you can use Ownable for a much shorter and simpler syntax:
17
- - @payment.owned_by? current_user # => true/false
18
-
21
+ @payment.owned_by? current_user # => true/false
19
22
 
20
23
  ## INSTALLATION
21
24
 
@@ -29,7 +32,6 @@ Then tell bundler to update the gems :
29
32
 
30
33
  $ bundle install
31
34
 
32
-
33
35
  ## USE
34
36
 
35
37
  In each Model define the associations from the Model to the "Owner" model (which will be User in this example)
@@ -38,33 +40,41 @@ Assuming a set of Model associations as follows:
38
40
 
39
41
  User has_many Orders has_one Invoice has_many Payments
40
42
 
43
+ Ownable is added to the Payment Model using "acts_as_ownable"
41
44
 
42
45
  class Payment < ActiveRecord::Base
43
46
  acts_as_ownable :through => [:invoice, :order, :user]
44
47
  ...
45
48
  end
46
49
 
47
- Now all Payment objects will have the instance methods:
50
+ The "acts_as_ownable" method call takes a hash with key :through with an Array value.
51
+ The Array is a list of the Model names that define the association path from the current Model back to,
52
+ and including the "Owner" model. Important note: The last item in the array is always the "Owner" model name.
48
53
 
49
- @payment.owned_by?(model_instance)
54
+ Now all Payment objects will have the instance methods:
50
55
 
51
- and
56
+ @payment.owned_by?(ActiveRecord Object)
57
+ @payment.owned_by?(Fixnum Id)
52
58
 
53
- @payment.owned_by_id?(id)
59
+ or using the belongs_to? alias
60
+
61
+ @payment.belongs_to?(ActiveRecord Object)
62
+ @payment.belongs_to?(Fixnum Id)
54
63
 
55
64
  Note that when declaring acts_as_ownable in the Models, the last Model name in the array must be the "Owner" model name.
56
65
  Also, this only works when each upstream association returns a single object. For example, the following will NOT work:
57
66
 
58
67
  Publisher has_many Authors has_and_belongs_to_many Books
59
68
 
69
+
60
70
  class Book < ActiveRecord::Base
61
71
  acts_as_ownable :through => [:author, :publisher]
62
72
  ...
63
73
  end
64
-
74
+
75
+
65
76
  @book.owned_by? Publisher.first
66
77
 
67
-
68
78
  This will not work because several authors may have co-authored a Book so there is multiple paths back to the "Owner" model (the Publisher).
69
79
  At this time this gem does not support these relationships.
70
80
 
@@ -16,39 +16,36 @@ module Ownable
16
16
 
17
17
  end #ClassMethods
18
18
 
19
- # Add owned_by? and owned_by_id? to ActiveRecord subclasses
19
+ # Add owned_by? and belongs_to? to ActiveRecord subclasses
20
20
  module InstanceMethods
21
21
 
22
- def owned_by? candidate
23
- owner = find_owner
22
+ def owned_by? candidate
23
+ owner = find_owner
24
24
 
25
- return false if (owner.nil? || candidate.nil?)
26
- candidate == owner
27
- end
28
-
29
- def owned_by_id? candidate_id
30
- owner = find_owner
31
-
32
- return false if (owner.nil? || candidate_id.nil?)
33
- candidate_id == owner.id
34
- end
35
-
25
+ return false if (owner.nil? || candidate.nil?)
26
+ return candidate == owner.id if candidate.is_a? Fixnum
27
+ return candidate == owner
28
+ end
36
29
 
37
- private
30
+ def belongs_to? candidate
31
+ owned_by? candidate
32
+ end
33
+
34
+ private
38
35
 
39
- def find_owner
40
- owner = self
41
-
42
- self.ownable_through.each do |class_name|
43
- begin
44
- owner = owner.send class_name
45
- rescue
46
- owner = nil
47
- end
48
- end
49
-
50
- return owner
51
- end
36
+ def find_owner
37
+ owner = self
38
+
39
+ self.ownable_through.each do |class_name|
40
+ begin
41
+ owner = owner.send class_name
42
+ rescue
43
+ owner = nil
44
+ end
45
+ end
46
+
47
+ return owner
48
+ end
52
49
 
53
50
  end #InstanceMethods
54
51
 
@@ -1,3 +1,3 @@
1
1
  module Ownable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["David Gonzalez"]
9
9
  s.email = ["davidjgonzalez@gmail.com"]
10
- s.homepage = "http://unicornless.com/gems/ownable"
10
+ s.homepage = "http://github.com/emp29/Ownable"
11
11
  s.summary = "Model ownership with Rails 3"
12
12
  s.description = "Model ownership with Rails 3"
13
13
 
@@ -15,8 +15,6 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_runtime_dependency 'rails', '~> 3.0.0'
17
17
 
18
- s.add_development_dependency "bundler", "~> 1.0.0"
19
-
20
18
  s.files = `git ls-files`.split("\n")
21
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Gonzalez
@@ -21,6 +21,7 @@ dependencies:
21
21
  name: rails
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ~>
26
27
  - !ruby/object:Gem::Version
@@ -31,20 +32,6 @@ dependencies:
31
32
  version: 3.0.0
32
33
  type: :runtime
33
34
  version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: bundler
36
- prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ~>
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 1
43
- - 0
44
- - 0
45
- version: 1.0.0
46
- type: :development
47
- version_requirements: *id002
48
35
  description: Model ownership with Rails 3
49
36
  email:
50
37
  - davidjgonzalez@gmail.com
@@ -55,7 +42,6 @@ extensions: []
55
42
  extra_rdoc_files: []
56
43
 
57
44
  files:
58
- - Gemfile
59
45
  - README.markdown
60
46
  - Rakefile
61
47
  - lib/ownable.rb
@@ -64,7 +50,7 @@ files:
64
50
  - lib/ownable/version.rb
65
51
  - ownable.gemspec
66
52
  has_rdoc: true
67
- homepage: http://unicornless.com/gems/ownable
53
+ homepage: http://github.com/emp29/Ownable
68
54
  licenses: []
69
55
 
70
56
  post_install_message:
@@ -73,6 +59,7 @@ rdoc_options: []
73
59
  require_paths:
74
60
  - lib
75
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
76
63
  requirements:
77
64
  - - ">="
78
65
  - !ruby/object:Gem::Version
@@ -80,6 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
67
  - 0
81
68
  version: "0"
82
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
83
71
  requirements:
84
72
  - - ">="
85
73
  - !ruby/object:Gem::Version
@@ -91,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
79
  requirements: []
92
80
 
93
81
  rubyforge_project:
94
- rubygems_version: 1.3.6
82
+ rubygems_version: 1.3.7
95
83
  signing_key:
96
84
  specification_version: 3
97
85
  summary: Model ownership with Rails 3
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source :rubygems
2
-
3
- gemspec