boundary 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ #
6
+ gem "activerecord", ">= 3.0.0"
7
+ gem "actionpack", ">= 3.0.0"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "rdoc", "~> 3.12"
13
+ gem "bundler", ">= 1.0.0"
14
+ gem "jeweler", "~> 1.8.4"
15
+ gem "minitest", "~> 3.2.0"
16
+ gem "rcov", "> 0"
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionpack (3.2.8)
5
+ activemodel (= 3.2.8)
6
+ activesupport (= 3.2.8)
7
+ builder (~> 3.0.0)
8
+ erubis (~> 2.7.0)
9
+ journey (~> 1.0.4)
10
+ rack (~> 1.4.0)
11
+ rack-cache (~> 1.2)
12
+ rack-test (~> 0.6.1)
13
+ sprockets (~> 2.1.3)
14
+ activemodel (3.2.8)
15
+ activesupport (= 3.2.8)
16
+ builder (~> 3.0.0)
17
+ activerecord (3.2.8)
18
+ activemodel (= 3.2.8)
19
+ activesupport (= 3.2.8)
20
+ arel (~> 3.0.2)
21
+ tzinfo (~> 0.3.29)
22
+ activesupport (3.2.8)
23
+ i18n (~> 0.6)
24
+ multi_json (~> 1.0)
25
+ arel (3.0.2)
26
+ builder (3.0.0)
27
+ erubis (2.7.0)
28
+ git (1.2.5)
29
+ hike (1.2.1)
30
+ i18n (0.6.1)
31
+ jeweler (1.8.4)
32
+ bundler (~> 1.0)
33
+ git (>= 1.2.5)
34
+ rake
35
+ rdoc
36
+ journey (1.0.4)
37
+ json (1.7.5)
38
+ minitest (3.2.0)
39
+ multi_json (1.3.6)
40
+ rack (1.4.1)
41
+ rack-cache (1.2)
42
+ rack (>= 0.4)
43
+ rack-test (0.6.1)
44
+ rack (>= 1.0)
45
+ rake (0.9.2.2)
46
+ rcov (1.0.0)
47
+ rdoc (3.12)
48
+ json (~> 1.4)
49
+ sprockets (2.1.3)
50
+ hike (~> 1.2)
51
+ rack (~> 1.0)
52
+ tilt (~> 1.1, != 1.3.0)
53
+ tilt (1.3.3)
54
+ tzinfo (0.3.33)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ actionpack (>= 3.0.0)
61
+ activerecord (>= 3.0.0)
62
+ bundler (>= 1.0.0)
63
+ jeweler (~> 1.8.4)
64
+ minitest (~> 3.2.0)
65
+ rcov (> 0)
66
+ rdoc (~> 3.12)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Laknath
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = Boundary
2
+
3
+ Introduces a thin wrapper to scope queries based on a fixed external parameter. Primary use case is to scope multi tenant apps based on the subdomain or some other attribute of the user.
4
+
5
+ == Contributing to boundary
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Laknath. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "boundary"
18
+ gem.homepage = "http://github.com/laknath/boundary"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A scope helper for multi-tenant apps}
21
+ gem.description = %Q{This gem is intended to provide a set of helpers to scope queries under a fixed parameter determined at the run time (ie: subdomain).}
22
+ gem.email = "blaknath@gmail.com"
23
+ gem.authors = ["Laknath"]
24
+ gem.add_dependency "activerecord", '>= 3.0.0'
25
+ gem.add_dependency "actionpack", '>= 3.0.0'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rdoc/task'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "boundary #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/boundary.gemspec ADDED
@@ -0,0 +1,76 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "boundary"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Laknath"]
12
+ s.date = "2012-08-31"
13
+ s.description = "This gem is intended to provide a set of helpers to scope queries under a fixed parameter determined at the run time (ie: subdomain)."
14
+ s.email = "blaknath@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "boundary.gemspec",
28
+ "lib/boundary.rb",
29
+ "lib/boundary/controller.rb",
30
+ "lib/boundary/model.rb",
31
+ "test/helper.rb",
32
+ "test/test_boundary.rb"
33
+ ]
34
+ s.homepage = "http://github.com/laknath/boundary"
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.21"
38
+ s.summary = "A scope helper for multi-tenant apps"
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
45
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
46
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
47
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
49
+ s.add_development_dependency(%q<minitest>, ["~> 3.2.0"])
50
+ s.add_development_dependency(%q<rcov>, ["> 0"])
51
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
52
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
53
+ else
54
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
55
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ s.add_dependency(%q<minitest>, ["~> 3.2.0"])
60
+ s.add_dependency(%q<rcov>, ["> 0"])
61
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
62
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
66
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
67
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
68
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
70
+ s.add_dependency(%q<minitest>, ["~> 3.2.0"])
71
+ s.add_dependency(%q<rcov>, ["> 0"])
72
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
73
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
74
+ end
75
+ end
76
+
data/lib/boundary.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'boundary/model'
2
+ require 'boundary/controller'
3
+
4
+ ActionController::Base.extend Boundary::Controller
5
+ ActiveRecord::Base.extend Boundary::Model
@@ -0,0 +1,45 @@
1
+ module Boundary
2
+ module Controller
3
+ # Adds a scope helper that call the model with a given parameter (ie: subdomain)
4
+ #
5
+ # Options:
6
+ # * <tt>:by</tt> - Target scope model name (default: :company)
7
+ # * <tt>:scope_function</tt> - Name of a function that returns the target scope object. This object should contain an ID column that matches with the foreign_id column of the scoped object (default: :current_company)
8
+ #
9
+ # bound_to :subscription, :by => :account, :scope_function => :current_employee
10
+ #
11
+ # In your actions,
12
+ #
13
+ # def show
14
+ # @subscription = bound_by_account { Subscription.find(params) }
15
+ # ...
16
+ # end
17
+ #
18
+ # For multiple scopes,
19
+ #
20
+ # bound_to :subscription, :by => :account, :scope_function => :current_employee
21
+ # bound_to :transaction, :by => :account, :scope_function => :current_employee
22
+ #
23
+ # def show
24
+ # @subscription = subscription_bound_by_account { Subscription.find(params) }
25
+ # @transaction = transaction_bound_by_account { Transaction.find(params) }
26
+ # end
27
+ def bound_to(model, *args, &block)
28
+ options = args.extract_options!
29
+
30
+ model_name = model.to_s.camelize
31
+ options[:by] ||= :company
32
+ options[:scope_function] ||= "current_#{options[:by]}"
33
+
34
+ helper_name = "#{model}_bound_by_#{options[:by]}"
35
+
36
+ self.class_eval <<-"end_eval", __FILE__, __LINE__
37
+ def #{helper_name}(&block)
38
+ #{model_name}.bound_by_#{options[:by]}(#{options[:scope_function]}.id, &block)
39
+ end
40
+
41
+ alias_method :bound_by_#{options[:by]}, :#{helper_name}
42
+ end_eval
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module Boundary
2
+ module Model
3
+ # Adds a scope helper for the specified scope (ie: company)
4
+ #
5
+ # Options:
6
+ # * <tt>:foreign_id</tt> - Foreign ID column name (default: :company_id)
7
+ #
8
+ # bound_by :account, :foreign_id => :employee_id
9
+ #
10
+ # Subscription.bound_by_account(account_id) { query }
11
+ def bound_by(scope, *args)
12
+ options = args.extract_options!
13
+
14
+ options[:foreign_id] ||= :company_id
15
+
16
+ self.class_eval <<-"end_eval", __FILE__, __LINE__
17
+ def self.bound_by_#{scope}(foreign_id, *args)
18
+ self.where(:#{options[:foreign_id]} => foreign_id).scoping do
19
+ yield if block_given?
20
+ end
21
+ end
22
+ end_eval
23
+ end
24
+ end
25
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'boundary'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestBoundary < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boundary
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Laknath
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-31 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ version_requirements: *id001
34
+ name: activerecord
35
+ prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ - 0
48
+ version: 3.0.0
49
+ version_requirements: *id002
50
+ name: actionpack
51
+ prerelease: false
52
+ - !ruby/object:Gem::Dependency
53
+ type: :development
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 31
60
+ segments:
61
+ - 3
62
+ - 12
63
+ version: "3.12"
64
+ version_requirements: *id003
65
+ name: rdoc
66
+ prerelease: false
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 23
75
+ segments:
76
+ - 1
77
+ - 0
78
+ - 0
79
+ version: 1.0.0
80
+ version_requirements: *id004
81
+ name: bundler
82
+ prerelease: false
83
+ - !ruby/object:Gem::Dependency
84
+ type: :development
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ hash: 63
91
+ segments:
92
+ - 1
93
+ - 8
94
+ - 4
95
+ version: 1.8.4
96
+ version_requirements: *id005
97
+ name: jeweler
98
+ prerelease: false
99
+ - !ruby/object:Gem::Dependency
100
+ type: :development
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ~>
105
+ - !ruby/object:Gem::Version
106
+ hash: 15
107
+ segments:
108
+ - 3
109
+ - 2
110
+ - 0
111
+ version: 3.2.0
112
+ version_requirements: *id006
113
+ name: minitest
114
+ prerelease: false
115
+ - !ruby/object:Gem::Dependency
116
+ type: :development
117
+ requirement: &id007 !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">"
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ version_requirements: *id007
127
+ name: rcov
128
+ prerelease: false
129
+ - !ruby/object:Gem::Dependency
130
+ type: :runtime
131
+ requirement: &id008 !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 7
137
+ segments:
138
+ - 3
139
+ - 0
140
+ - 0
141
+ version: 3.0.0
142
+ version_requirements: *id008
143
+ name: activerecord
144
+ prerelease: false
145
+ - !ruby/object:Gem::Dependency
146
+ type: :runtime
147
+ requirement: &id009 !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 7
153
+ segments:
154
+ - 3
155
+ - 0
156
+ - 0
157
+ version: 3.0.0
158
+ version_requirements: *id009
159
+ name: actionpack
160
+ prerelease: false
161
+ description: "This gem is intended to provide a set of helpers to scope queries under a fixed parameter determined at the run time (ie: subdomain)."
162
+ email: blaknath@gmail.com
163
+ executables: []
164
+
165
+ extensions: []
166
+
167
+ extra_rdoc_files:
168
+ - LICENSE.txt
169
+ - README.rdoc
170
+ files:
171
+ - .document
172
+ - Gemfile
173
+ - Gemfile.lock
174
+ - LICENSE.txt
175
+ - README.rdoc
176
+ - Rakefile
177
+ - VERSION
178
+ - boundary.gemspec
179
+ - lib/boundary.rb
180
+ - lib/boundary/controller.rb
181
+ - lib/boundary/model.rb
182
+ - test/helper.rb
183
+ - test/test_boundary.rb
184
+ homepage: http://github.com/laknath/boundary
185
+ licenses:
186
+ - MIT
187
+ post_install_message:
188
+ rdoc_options: []
189
+
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ hash: 3
198
+ segments:
199
+ - 0
200
+ version: "0"
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ hash: 3
207
+ segments:
208
+ - 0
209
+ version: "0"
210
+ requirements: []
211
+
212
+ rubyforge_project:
213
+ rubygems_version: 1.8.21
214
+ signing_key:
215
+ specification_version: 3
216
+ summary: A scope helper for multi-tenant apps
217
+ test_files: []
218
+