parameterize 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
1
  task :default => :spec
2
2
 
3
- require 'bundler'
4
- Bundler::GemHelper.install_tasks
5
-
3
+ require 'bundler/gem_tasks'
6
4
  require 'rspec/core/rake_task'
7
5
  RSpec::Core::RakeTask.new
@@ -1,5 +1,4 @@
1
1
  require 'active_record'
2
- require 'active_support/core_ext/array/extract_options'
3
2
  require 'active_support/core_ext/string/inflections'
4
3
 
5
4
  module Parameterize
@@ -7,26 +6,37 @@ module Parameterize
7
6
  # with the parameterized version of the given source field.
8
7
  #
9
8
  # @param [Symbol, String] source The field to parameterize
10
- def parameterize(source = :title)
9
+ def parameterize(source = :title, options = {})
11
10
  include InstanceMethods
12
11
 
13
- class_attribute :param_source_field
14
- before_validation :update_param
12
+ class_attribute :parameterize_config, :instance_writer => false
13
+ before_validation :parameterize, :if => :parameterize?
15
14
 
16
- self.param_source_field = source
15
+ self.parameterize_config = {
16
+ :source => source,
17
+ :target => :param,
18
+ :update => false
19
+ }.merge(options)
17
20
  end
18
21
 
19
22
  module InstanceMethods
20
- # Overwrite #to_param to return the generated param.
23
+ # Overwrite #to_param to return the parameterize target attribute. Falls
24
+ # back to the original implementation if for some reason the target
25
+ # attribute does not exist
21
26
  def to_param
22
- self.param
27
+ __send__(parameterize_config[:target]) || super
23
28
  end
24
29
 
25
30
  protected
26
31
 
27
- # Updates the param field with the parameterized version of the source field.
28
- def update_param
29
- self.param = __send__(self.class.param_source_field).to_s.parameterize
32
+ # Updates the target attribute with the parameterized version of the source attribute.
33
+ def parameterize
34
+ __send__(:"#{parameterize_config[:target]}=", __send__(parameterize_config[:source]).to_s.parameterize)
35
+ end
36
+
37
+ # Returns true if the source attribute should be parameterized.
38
+ def parameterize?
39
+ parameterize_config[:update] || __send__(parameterize_config[:target]).nil?
30
40
  end
31
41
  end
32
42
  end
@@ -2,9 +2,9 @@ require 'rspec/matchers'
2
2
 
3
3
  RSpec::Matchers.define :parameterize do |source|
4
4
  match do |model|
5
- model.class.include?(Parameterize::InstanceMethods) && model.class.param_source_field == source
5
+ model.class.include?(Parameterize::InstanceMethods) && model.class.parameterize_config[:source] == source
6
6
  end
7
-
7
+
8
8
  failure_message_for_should do |model|
9
9
  "expected #{model.class} to parameterize #{source}"
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module Parameterize
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency 'activerecord', '~> 3.0'
18
18
  s.add_dependency 'activesupport', '~> 3.0'
19
19
  s.add_development_dependency 'rake', '>= 0.8.7'
20
- s.add_development_dependency 'rspec', '~> 2.6'
20
+ s.add_development_dependency 'rspec', '~> 2.11'
21
21
  s.add_development_dependency 'sqlite3', '~> 1.3'
22
22
 
23
23
  s.files = `git ls-files`.split("\n")
@@ -1,33 +1,63 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Parameterize do
4
- it 'parameterizes the source field' do
5
- post = Post.new :title => "Hey Ho, Let's Go!"
4
+ it 'parameterizes the source attribute' do
5
+ post = Post.new title: "Hey Ho, Let's Go!"
6
6
  post.valid?
7
- post.param.should == 'hey-ho-let-s-go'
7
+ expect(post.param).to eq('hey-ho-let-s-go')
8
8
  end
9
-
10
- context 'with a different source field' do
11
- it 'parameterizes the source field' do
12
- user = User.new :name => 'Donald E. Knuth'
9
+
10
+ context 'with a custom source attribute and target attribute' do
11
+ it 'parameterizes the source attribute' do
12
+ user = User.new name: 'Donald E. Knuth'
13
13
  user.valid?
14
- user.param.should == 'donald-e-knuth'
14
+ expect(user.slug).to eq('donald-e-knuth')
15
15
  end
16
16
  end
17
-
18
- describe '#to_param' do
19
- it 'returns the param' do
20
- post = Post.new :title => "Hey Ho, Let's Go!"
17
+
18
+ context 'with an existing target attribute' do
19
+ it 'does not parameterize the source attribute' do
20
+ post = Post.new title: "Hey Ho, Let's Go!", param: 'hello-world'
21
21
  post.valid?
22
- post.to_param.should == 'hey-ho-let-s-go'
22
+ expect(post.param).to eq('hello-world')
23
+ end
24
+
25
+ context 'with :update set to true' do
26
+ it 'parameterizes the source attribute' do
27
+ user = User.new name: 'Donald E. Knuth', slug: 'john-doe'
28
+ user.valid?
29
+ expect(user.slug).to eq('donald-e-knuth')
30
+ end
23
31
  end
24
32
  end
25
-
33
+
34
+ describe '#to_param' do
35
+ it 'returns the target attribute' do
36
+ post = Post.new param: 'hello-world'
37
+ expect(post.to_param).to eq('hello-world')
38
+ end
39
+
40
+ context 'with a custom target attribute' do
41
+ it 'returns the custom target attribute' do
42
+ user = User.new slug: 'john-doe'
43
+ expect(user.to_param).to eq('john-doe')
44
+ end
45
+ end
46
+
47
+ context 'without a param' do
48
+ it 'returns the id' do
49
+ post = Post.new
50
+ post.id = 1
51
+ expect(post.to_param).to eq('1')
52
+ end
53
+ end
54
+ end
55
+
26
56
  describe 'matcher' do
27
57
  it 'checks if Post parameterized :title' do
28
58
  Post.new.should parameterize(:title)
29
59
  end
30
-
60
+
31
61
  it 'checks if User parameterized :name' do
32
62
  User.new.should parameterize(:name)
33
63
  end
@@ -4,9 +4,9 @@ require 'active_record'
4
4
  require 'parameterize'
5
5
  require 'parameterize/matcher'
6
6
 
7
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
7
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
8
8
  ActiveRecord::Migration.verbose = false
9
- ActiveRecord::Schema.define :version => 1 do
9
+ ActiveRecord::Schema.define version: 1 do
10
10
  create_table :posts do |t|
11
11
  t.string :title
12
12
  t.string :param
@@ -14,7 +14,7 @@ ActiveRecord::Schema.define :version => 1 do
14
14
 
15
15
  create_table :users do |t|
16
16
  t.string :name
17
- t.string :param
17
+ t.string :slug
18
18
  end
19
19
  end
20
20
 
@@ -23,5 +23,5 @@ class Post < ActiveRecord::Base
23
23
  end
24
24
 
25
25
  class User < ActiveRecord::Base
26
- parameterize :name
26
+ parameterize :name, target: :slug, update: true
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parameterize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70213142072060 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70213142072060
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: activesupport
27
- requirement: &70213142071520 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '3.0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70213142071520
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rake
38
- requirement: &70213142071040 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,21 +53,31 @@ dependencies:
43
53
  version: 0.8.7
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70213142071040
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rspec
49
- requirement: &70213142070540 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
53
68
  - !ruby/object:Gem::Version
54
- version: '2.6'
69
+ version: '2.11'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70213142070540
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.11'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: sqlite3
60
- requirement: &70213142070040 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,7 +85,12 @@ dependencies:
65
85
  version: '1.3'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70213142070040
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.3'
69
94
  description: It uses ActiveSupport's String#parameterize to create the slug. There
70
95
  are no validations. No slug history. No extra tables or models.
71
96
  email:
@@ -102,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
127
  version: '0'
103
128
  segments:
104
129
  - 0
105
- hash: -2472796874914442581
130
+ hash: 1014365171
106
131
  required_rubygems_version: !ruby/object:Gem::Requirement
107
132
  none: false
108
133
  requirements:
@@ -111,10 +136,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
136
  version: '0'
112
137
  segments:
113
138
  - 0
114
- hash: -2472796874914442581
139
+ hash: 1014365171
115
140
  requirements: []
116
141
  rubyforge_project: parameterize
117
- rubygems_version: 1.8.11
142
+ rubygems_version: 1.8.23
118
143
  signing_key:
119
144
  specification_version: 3
120
145
  summary: The simplest permalink/slug solution for ActiveRecord >= 3.0