ownable 0.1.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.
- data/Gemfile +3 -0
- data/README.markdown +73 -0
- data/Rakefile +17 -0
- data/lib/ownable.rb +2 -0
- data/lib/ownable/acts_as_ownable.rb +57 -0
- data/lib/ownable/railtie.rb +15 -0
- data/lib/ownable/version.rb +3 -0
- data/ownable.gemspec +24 -0
- metadata +99 -0
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
## INTRODUCTION
|
2
|
+
|
3
|
+
Ownable gem helps you verify the "ownership" of a associated models in Rails 3.
|
4
|
+
|
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
|
+
|
7
|
+
Ownable becomes incresa
|
8
|
+
|
9
|
+
For example:
|
10
|
+
|
11
|
+
User has_many Orders has_one Invoice has_many Payments
|
12
|
+
|
13
|
+
Rather than manually defining and then checking the association between User and Payment like:
|
14
|
+
- @payment.invoice.order.user == current_user # => true/false
|
15
|
+
|
16
|
+
you can use Ownable for a much shorter and simpler syntax:
|
17
|
+
- @payment.owned_by? current_user # => true/false
|
18
|
+
|
19
|
+
|
20
|
+
## INSTALLATION
|
21
|
+
|
22
|
+
Ownable is currently available only in Gem form and requires version >= 3.0 of Rails.
|
23
|
+
|
24
|
+
You just have to add the 'ownable' gem to your Gemfile
|
25
|
+
|
26
|
+
gem 'ownable'
|
27
|
+
|
28
|
+
Then tell bundler to update the gems :
|
29
|
+
|
30
|
+
$ bundle install
|
31
|
+
|
32
|
+
|
33
|
+
## USE
|
34
|
+
|
35
|
+
In each Model define the associations from the Model to the "Owner" model (which will be User in this example)
|
36
|
+
|
37
|
+
Assuming a set of Model associations as follows:
|
38
|
+
|
39
|
+
User has_many Orders has_one Invoice has_many Payments
|
40
|
+
|
41
|
+
|
42
|
+
class Payment < ActiveRecord::Base
|
43
|
+
acts_as_ownable :through => [:invoice, :order, :user]
|
44
|
+
...
|
45
|
+
end
|
46
|
+
|
47
|
+
Now all Payment objects will have the instance methods:
|
48
|
+
|
49
|
+
@payment.owned_by?(model_instance)
|
50
|
+
|
51
|
+
and
|
52
|
+
|
53
|
+
@payment.owned_by_id?(id)
|
54
|
+
|
55
|
+
Note that when declaring acts_as_ownable in the Models, the last Model name in the array must be the "Owner" model name.
|
56
|
+
Also, this only works when each upstream association returns a single object. For example, the following will NOT work:
|
57
|
+
|
58
|
+
Publisher has_many Authors has_and_belongs_to_many Books
|
59
|
+
|
60
|
+
class Book < ActiveRecord::Base
|
61
|
+
acts_as_ownable :through => [:author, :publisher]
|
62
|
+
...
|
63
|
+
end
|
64
|
+
|
65
|
+
@book.owned_by? Publisher.first
|
66
|
+
|
67
|
+
|
68
|
+
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
|
+
At this time this gem does not support these relationships.
|
70
|
+
|
71
|
+
## THANKS
|
72
|
+
|
73
|
+
Thanks to jlecour's (http://github.com/jlecour) geokit-rails project for helping me understand how to build a gem that plays well with Rails 3.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
#load 'test/tasks.rake'
|
6
|
+
|
7
|
+
#desc 'Default: run unit tests.'
|
8
|
+
#task :default => :test
|
9
|
+
|
10
|
+
desc 'Generate documentation for the Ownable gem.'
|
11
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'Ownable'
|
14
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
15
|
+
rdoc.rdoc_files.include('README')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
data/lib/ownable.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Ownable
|
5
|
+
|
6
|
+
# Add acts_as_ownable to ActiveRecord subclasses
|
7
|
+
module ActsAsOwnable extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def acts_as_ownable(options = {})
|
12
|
+
cattr_accessor :ownable_through
|
13
|
+
|
14
|
+
self.ownable_through = options[:through] || [':user']
|
15
|
+
end
|
16
|
+
|
17
|
+
end #ClassMethods
|
18
|
+
|
19
|
+
# Add owned_by? and owned_by_id? to ActiveRecord subclasses
|
20
|
+
module InstanceMethods
|
21
|
+
|
22
|
+
def owned_by? candidate
|
23
|
+
owner = find_owner
|
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
|
+
|
36
|
+
|
37
|
+
private
|
38
|
+
|
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
|
52
|
+
|
53
|
+
end #InstanceMethods
|
54
|
+
|
55
|
+
end #ActsAsOwnable
|
56
|
+
|
57
|
+
end #Ownable
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'ownable'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Ownable
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
initializer 'ownable.insert_into_active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
ActiveRecord::Base.send :extend, Ownable::ActsAsOwnable::ClassMethods
|
10
|
+
ActiveRecord::Base.send :include, Ownable::ActsAsOwnable::InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/ownable.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/ownable/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "ownable"
|
6
|
+
s.version = Ownable::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["David Gonzalez"]
|
9
|
+
s.email = ["davidjgonzalez@gmail.com"]
|
10
|
+
s.homepage = "http://unicornless.com/gems/ownable"
|
11
|
+
s.summary = "Model ownership with Rails 3"
|
12
|
+
s.description = "Model ownership with Rails 3"
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
|
16
|
+
s.add_runtime_dependency 'rails', '~> 3.0.0'
|
17
|
+
|
18
|
+
s.add_development_dependency "bundler", "~> 1.0.0"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ownable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- David Gonzalez
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-04 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 3.0.0
|
32
|
+
type: :runtime
|
33
|
+
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
|
+
description: Model ownership with Rails 3
|
49
|
+
email:
|
50
|
+
- davidjgonzalez@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- Gemfile
|
59
|
+
- README.markdown
|
60
|
+
- Rakefile
|
61
|
+
- lib/ownable.rb
|
62
|
+
- lib/ownable/acts_as_ownable.rb
|
63
|
+
- lib/ownable/railtie.rb
|
64
|
+
- lib/ownable/version.rb
|
65
|
+
- ownable.gemspec
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://unicornless.com/gems/ownable
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 1
|
88
|
+
- 3
|
89
|
+
- 6
|
90
|
+
version: 1.3.6
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.3.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Model ownership with Rails 3
|
98
|
+
test_files: []
|
99
|
+
|