sid 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -14,6 +14,7 @@ In the models, add:
14
14
 
15
15
  class Page < ActiveRecord::Base
16
16
  has_sid :account_id
17
+ # has_sid, :account_id, :sid_column => :some_other_column # you can specify what column it will consider the sid column. Defaults to `sid`
17
18
  end
18
19
 
19
20
  ## Contributing to sid
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.3
@@ -3,6 +3,7 @@ module Sid
3
3
  extend ActiveSupport::Concern
4
4
  included do
5
5
  before_create :set_sid_column
6
+ validates sid_config[:sid_column], :uniqueness => true
6
7
  end
7
8
  end
8
9
  end
@@ -1,14 +1,23 @@
1
1
  module Sid
2
2
  module InstanceMethods
3
+ def sid_column=(value)
4
+ self.send :"#{self.class.sid_config[:sid_column]}=", value
5
+ end
6
+
7
+ def sid_column
8
+ self.send :"#{self.class.sid_config[:sid_column]}"
9
+ end
10
+
3
11
  private
4
12
 
5
13
  def set_sid_column
6
- collection = self.class.where(self.class.scoped_by => self.send(self.class.scoped_by))
14
+ scoped_by = self.class.sid_config[:scoped_by]
15
+ collection = self.class.where(scoped_by => self.send(scoped_by))
7
16
 
8
17
  if collection.count.zero?
9
- self.sid = 1
18
+ self.sid_column = 1
10
19
  else
11
- self.sid = collection.reorder('id').last.sid + 1
20
+ self.sid_column = collection.reorder('id').last.sid_column + 1
12
21
  end
13
22
  end
14
23
  end
data/lib/sid.rb CHANGED
@@ -2,9 +2,11 @@ require 'sid/class_methods'
2
2
  require 'sid/instance_methods'
3
3
 
4
4
  module Sid
5
- def has_sid(scoped_by)
6
- cattr_accessor :scoped_by
7
- self.scoped_by = scoped_by
5
+ def has_sid(scoped_by, options={})
6
+ cattr_accessor :sid_config
7
+ self.sid_config={}
8
+ self.sid_config[:scoped_by] = scoped_by
9
+ self.sid_config[:sid_column] = options[:sid_column] || :sid
8
10
 
9
11
  include Sid::ClassMethods
10
12
  include Sid::InstanceMethods
data/sid.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sid}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ramon Tayag"]
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/sid/class_methods.rb",
32
32
  "lib/sid/instance_methods.rb",
33
33
  "sid.gemspec",
34
+ "spec/fixtures/category.rb",
34
35
  "spec/fixtures/page.rb",
35
36
  "spec/sid_spec.rb",
36
37
  "spec/spec_helper.rb",
@@ -0,0 +1,3 @@
1
+ class Category < ActiveRecord::Base
2
+ has_sid :account_id, :sid_column => :alternative_sid
3
+ end
data/spec/sid_spec.rb CHANGED
@@ -18,4 +18,8 @@ describe 'Sid' do
18
18
  Page.find_by_sid(2).destroy
19
19
  Page.create(:account_id => 1).sid.should == 5
20
20
  end
21
+
22
+ it "should allow setting of sid column" do
23
+ Category.create(:account_id => 1).alternative_sid.should == 1
24
+ end
21
25
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_record'
5
5
  require 'rspec'
6
6
  require 'sid'
7
7
  require 'spec/fixtures/page'
8
+ require 'spec/fixtures/category'
8
9
 
9
10
  # Requires supporting files with custom matchers and macros, etc,
10
11
  # in ./support/ and its subdirectories.
@@ -21,13 +22,17 @@ RSpec.configure do |config|
21
22
  end
22
23
 
23
24
  def reset_database
24
- %W(pages).each do |table_name|
25
+ %W(pages categories).each do |table_name|
25
26
  ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS '#{table_name}'")
26
27
  end
27
28
  ActiveRecord::Base.connection.create_table(:pages) do |t|
28
29
  t.string :name
29
30
  t.integer :account_id
30
31
  t.integer :sid
31
- t.integer :sid_alternative
32
+ end
33
+ ActiveRecord::Base.connection.create_table(:categories) do |t|
34
+ t.string :name
35
+ t.integer :account_id
36
+ t.integer :alternative_sid
32
37
  end
33
38
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ramon Tayag
@@ -162,6 +162,7 @@ files:
162
162
  - lib/sid/class_methods.rb
163
163
  - lib/sid/instance_methods.rb
164
164
  - sid.gemspec
165
+ - spec/fixtures/category.rb
165
166
  - spec/fixtures/page.rb
166
167
  - spec/sid_spec.rb
167
168
  - spec/spec_helper.rb