mad_id 1.0.1 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66e2d89517f6426565c0664badeb7157ab79e1b7
4
- data.tar.gz: 2146b87b423e6c912683c0421622b6491dd419c7
3
+ metadata.gz: fed2ca12f59a902c0578e6fa8e8001fa80e45ae9
4
+ data.tar.gz: 2b714b98ea52f14774df6b7e731bf0241595f12c
5
5
  SHA512:
6
- metadata.gz: 0b1507dae5c4d98528f73bca48acd7625ac7cee6c8f52032ee1c242361b6e045e1c1317dbd10ed61367205dc34ec43bedb4717fe3f08dad53f4cc026ae1ba500
7
- data.tar.gz: 4cd65a70acf6023f8b5c4d15dbe17e8a77a23863057602aa401ee266e0f32ccae45ea65f39bb43e707109a66718e13d162101d715639c520f1c7ccc008f0422b
6
+ metadata.gz: 3c9f2fe65978d9621e7949e80b112190268fa7fa84d71e883ef8e89283acc575e55c1ba4afe3a8fdd6eb914a424402364c890dd596b630b555cbe448a5295efa
7
+ data.tar.gz: fd30b90a66517bafae7fd49a7ede19234e442f090ae9299bc13df46b611cd1beb87c1c0dc3b0e8080b0cf39144a4224fc0ef57f8479c69d74b5bd98b3a5152df
@@ -1,10 +1,18 @@
1
1
  module MadID::FinderMethods
2
2
 
3
3
  def find_by_mad_id!(id)
4
- find_by!(identifier: id.to_s.downcase)
4
+ find_by!(mad_id_column => id.to_s.downcase)
5
5
  end
6
6
  def find_by_mad_id(id)
7
- find_by(identifier: id.to_s.downcase)
7
+ find_by(mad_id_column => id.to_s.downcase)
8
+ end
9
+
10
+ def mad_id_column
11
+ @mad_id_column || 'id'
12
+ end
13
+
14
+ def mad_id_column=(val)
15
+ @mad_id_column = val
8
16
  end
9
17
 
10
18
  end
@@ -3,6 +3,12 @@ module MadID
3
3
  module IdentityMethods
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ module UrlMethods
7
+ def to_param
8
+ self.identifier
9
+ end
10
+ end
11
+
6
12
  included do
7
13
  before_create :set_identifier
8
14
  attr_readonly :identifier
@@ -20,9 +26,13 @@ module MadID
20
26
  self.identifier[0..11]
21
27
  end
22
28
 
23
- def to_param
24
- self.identifier
29
+ def identifier=(value)
30
+ write_attribute(self.class.mad_id_column, value)
25
31
  end
26
- end
27
32
 
33
+ def identifier
34
+ read_attribute(self.class.mad_id_column)
35
+ end
36
+
37
+ end
28
38
  end
@@ -1,3 +1,3 @@
1
1
  module MadID
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/mad_id.rb CHANGED
@@ -30,11 +30,13 @@ module MadID
30
30
  end
31
31
 
32
32
  included do
33
- def self.identify_with(value)
33
+ extend(MadID::FinderMethods)
34
+ def self.identify_with(value, options = {})
35
+ self.mad_id_column = options[:column] || 'identifier'
34
36
  @identifier = value
35
37
  MadID.registry[value.to_s] = self
36
38
  self.send(:include, MadID::IdentityMethods)
37
- self.extend(MadID::FinderMethods)
39
+ self.send(:include, MadID::IdentityMethods::UrlMethods) unless options[:to_param] == false # include by default
38
40
  end
39
41
  end
40
42
  end
data/spec/mad_id_spec.rb CHANGED
@@ -9,7 +9,12 @@ class LittlePony < Pony
9
9
  end
10
10
 
11
11
  class GreatPony < Pony
12
- identify_with :grtpny
12
+ identify_with :grtpny, to_param: false
13
+ end
14
+
15
+ class Dog < ActiveRecord::Base
16
+ include MadID
17
+ identify_with :dog, column: 'mad_id'
13
18
  end
14
19
 
15
20
  describe MadID do
@@ -18,6 +23,16 @@ describe MadID do
18
23
  expect(Pony.respond_to?(:identify_with)).to be(true)
19
24
  end
20
25
 
26
+ describe 'default finders' do
27
+ let(:pony) { Pony.create }
28
+ it { expect(Pony.mad_id_column).to eql('id') }
29
+ it { expect(Pony.find_by_mad_id(pony.id)).to eql(pony) }
30
+ end
31
+
32
+ describe 'configurable column' do
33
+ it { expect(Dog.mad_id_column).to eql('mad_id') }
34
+ end
35
+
21
36
  it 'wont set any callbacks' do
22
37
  expect(Pony._create_callbacks.select {|cb| cb.filter == :set_identifier}).to be_empty
23
38
  end
@@ -48,6 +63,13 @@ describe MadID do
48
63
  it 'will start with the #identify_with argument' do
49
64
  expect(subject.identifier).to match(/pny-.+/)
50
65
  end
66
+
67
+ describe 'custom identifier column' do
68
+ subject { Dog.new }
69
+ before { subject.set_identifier }
70
+ it { expect(subject.mad_id).to_not be_nil }
71
+ it { expect(subject.identifier).to eql(subject.mad_id) }
72
+ end
51
73
  end
52
74
 
53
75
  describe 'callbacks' do
@@ -71,6 +93,16 @@ describe MadID do
71
93
  end
72
94
  end
73
95
 
96
+ describe "to param" do
97
+ let!(:little_pony) { LittlePony.create }
98
+ let!(:great_pony) { GreatPony.create }
99
+
100
+ it do
101
+ expect(little_pony.to_param).to eql(little_pony.identifier)
102
+ expect(great_pony.to_param).to eql(great_pony.id.to_s)
103
+ end
104
+ end
105
+
74
106
  describe 'Registry' do
75
107
  it 'includes identifiable objects with stringified key' do
76
108
  expect(MadID.registry).to include('pny' => LittlePony)
@@ -2,4 +2,7 @@ ActiveRecord::Schema.define(version: 1) do
2
2
  create_table "ponies", force: true do |t|
3
3
  t.string "identifier"
4
4
  end
5
+ create_table "dogs", force: true do |t|
6
+ t.string "mad_id"
7
+ end
5
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mad_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Brillert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord