sid 0.0.1

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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm @sid
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ gem "activerecord"
3
+
4
+ # Add dependencies to develop your gem here.
5
+ # Include everything needed to run rake, tests, features, etc.
6
+ group :development do
7
+ gem 'machinist', '>= 2.0.0.beta2'
8
+ gem 'sqlite3'
9
+ gem 'guard-rspec'
10
+ gem "rspec", '2.6.0'
11
+ gem "bundler"
12
+ gem "jeweler", '1.6.4'
13
+ gem "rcov"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.9)
5
+ activesupport (= 3.0.9)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.9)
9
+ activemodel (= 3.0.9)
10
+ activesupport (= 3.0.9)
11
+ arel (~> 2.0.10)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.9)
14
+ arel (2.0.10)
15
+ builder (2.1.2)
16
+ diff-lcs (1.1.2)
17
+ git (1.2.5)
18
+ guard (0.5.1)
19
+ thor (~> 0.14.6)
20
+ guard-rspec (0.4.0)
21
+ guard (>= 0.4.0)
22
+ i18n (0.5.0)
23
+ jeweler (1.6.4)
24
+ bundler (~> 1.0)
25
+ git (>= 1.2.5)
26
+ rake
27
+ machinist (2.0.0.beta2)
28
+ rake (0.9.2)
29
+ rcov (0.9.9)
30
+ rspec (2.6.0)
31
+ rspec-core (~> 2.6.0)
32
+ rspec-expectations (~> 2.6.0)
33
+ rspec-mocks (~> 2.6.0)
34
+ rspec-core (2.6.4)
35
+ rspec-expectations (2.6.0)
36
+ diff-lcs (~> 1.1.2)
37
+ rspec-mocks (2.6.0)
38
+ sqlite3 (1.3.3)
39
+ thor (0.14.6)
40
+ tzinfo (0.3.29)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ activerecord
47
+ bundler
48
+ guard-rspec
49
+ jeweler (= 1.6.4)
50
+ machinist (>= 2.0.0.beta2)
51
+ rcov
52
+ rspec (= 2.6.0)
53
+ sqlite3
data/Guardfile ADDED
@@ -0,0 +1,20 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
13
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
14
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+ watch('config/routes.rb') { "spec/routing" }
17
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
18
+ # Capybara request specs
19
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ramon Tayag
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.md ADDED
@@ -0,0 +1,32 @@
1
+ # sid
2
+
3
+ Useful for multi-tenant apps where the data lives on the same database, but you want the elements of a tenant to associate with each other via relative ids, not the absolute database id. This is also known as the scoped id (sid).
4
+
5
+ ## Why?
6
+
7
+ I wanted elements of a certain account to connect to one another via relative ids, not absolute. Why? Because if I wanted to just dump the data of that account, and load it back later, I didn't want to worry about the ids changing. The relations would stay intact.
8
+
9
+ ## Installation and Usage
10
+
11
+ Add a `sid` integer column to the tables you want to have this. These tables should `belong_to` the thing you want to scope by. It's typically the `account_id`. Don't forget to add an index too!
12
+
13
+ In the models, add:
14
+
15
+ class Page < ActiveRecord::Base
16
+ has_sid :account_id
17
+ end
18
+
19
+ ## Contributing to sid
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
23
+ * Fork the project
24
+ * Start a feature/bugfix branch
25
+ * Commit and push until you are happy with your contribution
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * 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.
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2011 Ramon Tayag. See LICENSE.txt for
32
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
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 = "sid"
18
+ gem.homepage = "http://github.com/ramontayag/sid"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Have a separate, relative id column in your table}
21
+ gem.description = %Q{Useful for multi-tenant apps where the data lives on the same database, but you want the elements of a tenant to associate with each other via relative ids, not the absolute database id.}
22
+ gem.email = "ramon@tayag.net"
23
+ gem.authors = ["Ramon Tayag"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "sid #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/sid.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'sid/class_methods'
2
+ require 'sid/instance_methods'
3
+
4
+ module Sid
5
+ def has_sid(scoped_by)
6
+ cattr_accessor :scoped_by
7
+ self.scoped_by = scoped_by
8
+
9
+ include Sid::ClassMethods
10
+ include Sid::InstanceMethods
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Base.extend Sid
@@ -0,0 +1,8 @@
1
+ module Sid
2
+ module ClassMethods
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ before_create :set_sid_column
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ module Sid
2
+ module InstanceMethods
3
+ private
4
+
5
+ def set_sid_column
6
+ collection = self.class.where(self.class.scoped_by => self.send(self.class.scoped_by))
7
+
8
+ if collection.count.zero?
9
+ self.sid = 1
10
+ else
11
+ self.sid = collection.reorder('id').last.sid + 1
12
+ end
13
+ end
14
+ end
15
+ end
data/sid.gemspec ADDED
@@ -0,0 +1,78 @@
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 = %q{sid}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ramon Tayag"]
12
+ s.date = %q{2011-07-24}
13
+ s.description = %q{Useful for multi-tenant apps where the data lives on the same database, but you want the elements of a tenant to associate with each other via relative ids, not the absolute database id.}
14
+ s.email = %q{ramon@tayag.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "Guardfile",
26
+ "LICENSE.txt",
27
+ "README.md",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "lib/sid.rb",
31
+ "lib/sid/class_methods.rb",
32
+ "lib/sid/instance_methods.rb",
33
+ "sid.gemspec",
34
+ "spec/fixtures/page.rb",
35
+ "spec/sid_spec.rb",
36
+ "spec/spec_helper.rb",
37
+ "spec/support/blueprints.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/ramontayag/sid}
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.6.2}
43
+ s.summary = %q{Have a separate, relative id column in your table}
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
50
+ s.add_development_dependency(%q<machinist>, [">= 2.0.0.beta2"])
51
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
52
+ s.add_development_dependency(%q<guard-rspec>, [">= 0"])
53
+ s.add_development_dependency(%q<rspec>, ["= 2.6.0"])
54
+ s.add_development_dependency(%q<bundler>, [">= 0"])
55
+ s.add_development_dependency(%q<jeweler>, ["= 1.6.4"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<activerecord>, [">= 0"])
59
+ s.add_dependency(%q<machinist>, [">= 2.0.0.beta2"])
60
+ s.add_dependency(%q<sqlite3>, [">= 0"])
61
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, ["= 2.6.0"])
63
+ s.add_dependency(%q<bundler>, [">= 0"])
64
+ s.add_dependency(%q<jeweler>, ["= 1.6.4"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<activerecord>, [">= 0"])
69
+ s.add_dependency(%q<machinist>, [">= 2.0.0.beta2"])
70
+ s.add_dependency(%q<sqlite3>, [">= 0"])
71
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
72
+ s.add_dependency(%q<rspec>, ["= 2.6.0"])
73
+ s.add_dependency(%q<bundler>, [">= 0"])
74
+ s.add_dependency(%q<jeweler>, ["= 1.6.4"])
75
+ s.add_dependency(%q<rcov>, [">= 0"])
76
+ end
77
+ end
78
+
@@ -0,0 +1,3 @@
1
+ class Page < ActiveRecord::Base
2
+ has_sid :account_id
3
+ end
data/spec/sid_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Sid' do
4
+ before do
5
+ reset_database
6
+ end
7
+
8
+ it "should auto increment the sid field of a model" do
9
+ Page.create(:account_id => 1).sid.should == 1
10
+ Page.create(:account_id => 1).sid.should == 2
11
+ Page.create(:account_id => 1).destroy
12
+ Page.create(:account_id => 1).sid.should == 3
13
+
14
+ Page.create(:account_id => 2).sid.should == 1
15
+ Page.create(:account_id => 1).sid.should == 4
16
+ Page.create(:account_id => 2).sid.should == 2
17
+
18
+ Page.find_by_sid(2).destroy
19
+ Page.create(:account_id => 1).sid.should == 5
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'rspec'
6
+ require 'sid'
7
+ require 'spec/fixtures/page'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ #Allow to connect to SQLite
14
+ ActiveRecord::Base.establish_connection(
15
+ :adapter => "sqlite3",
16
+ :database => ":memory:"
17
+ )
18
+
19
+ RSpec.configure do |config|
20
+
21
+ end
22
+
23
+ def reset_database
24
+ %W(pages).each do |table_name|
25
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS '#{table_name}'")
26
+ end
27
+ ActiveRecord::Base.connection.create_table(:pages) do |t|
28
+ t.string :name
29
+ t.integer :account_id
30
+ t.integer :sid
31
+ t.integer :sid_alternative
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ require 'machinist/active_record'
2
+
3
+ Page.blueprint do
4
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ramon Tayag
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-24 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: activerecord
33
+ version_requirements: *id001
34
+ prerelease: false
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 62196455
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ - beta
48
+ - 2
49
+ version: 2.0.0.beta2
50
+ name: machinist
51
+ version_requirements: *id002
52
+ prerelease: false
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ name: sqlite3
65
+ version_requirements: *id003
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: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: guard-rspec
79
+ version_requirements: *id004
80
+ prerelease: false
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - "="
87
+ - !ruby/object:Gem::Version
88
+ hash: 23
89
+ segments:
90
+ - 2
91
+ - 6
92
+ - 0
93
+ version: 2.6.0
94
+ name: rspec
95
+ version_requirements: *id005
96
+ prerelease: false
97
+ - !ruby/object:Gem::Dependency
98
+ type: :development
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ name: bundler
109
+ version_requirements: *id006
110
+ prerelease: false
111
+ - !ruby/object:Gem::Dependency
112
+ type: :development
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - "="
117
+ - !ruby/object:Gem::Version
118
+ hash: 7
119
+ segments:
120
+ - 1
121
+ - 6
122
+ - 4
123
+ version: 1.6.4
124
+ name: jeweler
125
+ version_requirements: *id007
126
+ prerelease: false
127
+ - !ruby/object:Gem::Dependency
128
+ type: :development
129
+ requirement: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ name: rcov
139
+ version_requirements: *id008
140
+ prerelease: false
141
+ description: Useful for multi-tenant apps where the data lives on the same database, but you want the elements of a tenant to associate with each other via relative ids, not the absolute database id.
142
+ email: ramon@tayag.net
143
+ executables: []
144
+
145
+ extensions: []
146
+
147
+ extra_rdoc_files:
148
+ - LICENSE.txt
149
+ - README.md
150
+ files:
151
+ - .document
152
+ - .rspec
153
+ - .rvmrc
154
+ - Gemfile
155
+ - Gemfile.lock
156
+ - Guardfile
157
+ - LICENSE.txt
158
+ - README.md
159
+ - Rakefile
160
+ - VERSION
161
+ - lib/sid.rb
162
+ - lib/sid/class_methods.rb
163
+ - lib/sid/instance_methods.rb
164
+ - sid.gemspec
165
+ - spec/fixtures/page.rb
166
+ - spec/sid_spec.rb
167
+ - spec/spec_helper.rb
168
+ - spec/support/blueprints.rb
169
+ has_rdoc: true
170
+ homepage: http://github.com/ramontayag/sid
171
+ licenses:
172
+ - MIT
173
+ post_install_message:
174
+ rdoc_options: []
175
+
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ hash: 3
184
+ segments:
185
+ - 0
186
+ version: "0"
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ none: false
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ hash: 3
193
+ segments:
194
+ - 0
195
+ version: "0"
196
+ requirements: []
197
+
198
+ rubyforge_project:
199
+ rubygems_version: 1.6.2
200
+ signing_key:
201
+ specification_version: 3
202
+ summary: Have a separate, relative id column in your table
203
+ test_files: []
204
+