acts_more_seo 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -6,3 +6,5 @@
6
6
  * Way cooler ;)
7
7
  = Version 0.2.2
8
8
  * Not raising no table error
9
+ = Version 0.2.3
10
+ * Major bug fixes, API changes
data/README.md CHANGED
@@ -53,9 +53,9 @@ However, if you do so - it is highly recommended to create string seo_url column
53
53
 
54
54
  add_column :model, :seo_url, :string
55
55
 
56
- so you can search via find_by_seo_url method:
56
+ so you can search via seo method:
57
57
 
58
- CoolElement.find_by_seo_url(params[:id])
58
+ CoolElement.find_by_seo(params[:id])
59
59
 
60
60
  You don't need to update seo_url, gem will hook up with this field automatically if it exists.
61
61
 
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rake'
4
4
  require 'echoe'
5
5
 
6
- Echoe.new('acts_more_seo', '0.2.2') do |p|
6
+ Echoe.new('acts_more_seo', '0.2.3') do |p|
7
7
  p.description = "Gem makes your ActiveRecord models more SEO friendly. Changes URL to look way better"
8
8
  p.url = "https://github.com/mensfeld/Css-Image-Embedder"
9
9
  p.author = "Maciej Mensfeld"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{acts_more_seo}
5
- s.version = "0.2.2"
5
+ s.version = "0.2.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Maciej Mensfeld}]
data/lib/acts_more_seo.rb CHANGED
@@ -46,40 +46,33 @@ module Acts
46
46
 
47
47
  # Overwrite default to_param - should return nice url
48
48
  def to_param
49
- self.url
49
+ self.to_url
50
50
  end
51
51
 
52
52
  # Lets return nice and smooth url
53
- def url
54
- # If there is more than one SEO column - prepare url "text" part
55
- if self.class.seo_columns.is_a?(Array)
56
- seo_parts = self.class.seo_columns.collect do |field|
57
- el = self.send(field)
58
- el.to_url if el
59
- end
60
- seo_parts.delete_if{|el| el.to_s.length == 0}
61
- seo_path = seo_parts.join('-')
62
- else
63
- el = self.send(self.class.seo_columns)
64
- seo_path = el ? el.to_url : ''
65
- end
53
+ def to_url
54
+ seo_path = seo_text_part
55
+ seo_path = "#{self.id}#{"-#{seo_path}" if seo_path && seo_path.length > 0}"
56
+ seo_path.length > 0 ? seo_path : "#{self.id}"
57
+ end
66
58
 
67
- if self.class.seo_use_id
68
- seo_path = "#{self.id}#{"-#{seo_path}" if seo_path && seo_path.length > 0}"
59
+ def to_seo
60
+ if self.seo_use_id
61
+ to_url
62
+ else
63
+ seo_text_part.length > 0 ? seo_text_part : "#{self.id}"
69
64
  end
70
-
71
- seo_path.length > 0 ? seo_path : "#{self.id}"
72
65
  end
73
66
 
74
67
  def update_seo_url
75
68
  if self.class.seo_url_table?
76
- self.update_column(:seo_url, self.to_param)
69
+ self.update_column(:seo_url, to_seo)
77
70
  end
78
71
  end
79
72
 
80
73
  def set_seo_url
81
74
  if self.class.seo_url_table?
82
- self.update_column(:seo_url, self.to_param)
75
+ self.update_column(:seo_url, to_seo)
83
76
  end
84
77
  end
85
78
 
@@ -89,17 +82,36 @@ module Acts
89
82
  self.column_names.include?("#{:seo_url}")
90
83
  end
91
84
 
92
- def find(*args)
93
- super
94
- rescue ActiveRecord::RecordNotFound
95
- if !self.seo_use_id && !(el = find_by_seo_url(args.first, :first)).nil?
96
- el
85
+ def find_by_seo(id)
86
+ if self.seo_use_id
87
+ find(id)
97
88
  else
98
- raise ActiveRecord::RecordNotFound
89
+ el = find_by_seo_url id, :first
90
+ raise ActiveRecord::RecordNotFound unless el
91
+ el
99
92
  end
100
93
  end
101
94
 
102
95
  end
96
+
97
+ private
98
+
99
+ def seo_text_part
100
+ # If there is more than one SEO column - prepare url "text" part
101
+ if self.class.seo_columns.is_a?(Array)
102
+ seo_parts = self.class.seo_columns.collect do |field|
103
+ el = self.send(field)
104
+ el.to_url if el
105
+ end
106
+ seo_parts.delete_if{|el| el.to_s.length == 0}
107
+ seo_path = seo_parts.join('-')
108
+ else
109
+ el = self.send(self.class.seo_columns)
110
+ seo_path = el ? el.to_url : ''
111
+ end
112
+ seo_path
113
+ end
114
+
103
115
  end
104
116
  end
105
117
  end
@@ -25,10 +25,10 @@ describe CoolElement do
25
25
  context "when we have a class which has use_id => true" do
26
26
  it "should find element unless id is corrupted" do
27
27
  a = subject.create(:name => 'bla bla bla')
28
- subject.find(a.id).should eql(a)
29
- subject.find(a.to_param).should eql(a)
30
- subject.find("#{a.to_param}aaa").should eql(a)
31
- lambda { subject.find("134534dass") }.should raise_error(ActiveRecord::RecordNotFound)
28
+ subject.find_by_seo(a.id).should eql(a)
29
+ subject.find_by_seo(a.to_param).should eql(a)
30
+ subject.find_by_seo("#{a.to_param}aaa").should eql(a)
31
+ lambda { subject.find_by_seo("134534dass") }.should raise_error(ActiveRecord::RecordNotFound)
32
32
  end
33
33
  end
34
34
 
@@ -65,12 +65,12 @@ describe CoolerElement do
65
65
 
66
66
  it "should be able to find existing element" do
67
67
  a = subject.create
68
- subject.find(a.id).should eql(a)
68
+ subject.find_by_seo(a.id).should eql(a)
69
69
  end
70
70
 
71
71
  it "should raise error when there is no element" do
72
72
  a = subject.create
73
- lambda { subject.find(a.id+1) }.should raise_error(ActiveRecord::RecordNotFound)
73
+ lambda { subject.find_by_seo(a.id+1) }.should raise_error(ActiveRecord::RecordNotFound)
74
74
  end
75
75
  end
76
76
 
@@ -78,7 +78,7 @@ describe CoolerElement do
78
78
  it "should return nice url" do
79
79
  a = subject.create(:title => 'bla bla bla')
80
80
  a.to_param.should eql("#{a.id}-bla-bla-bla")
81
- subject.find(a.to_param).should eql(a)
81
+ subject.find_by_seo(a.to_param).should eql(a)
82
82
  end
83
83
 
84
84
  context "and there are some url-not-friendly letters" do
@@ -125,7 +125,7 @@ describe BestElement do
125
125
  context "but surname and title exists" do
126
126
  it "should return nice looking url " do
127
127
  a = subject.create({:name => 'mensfeld', :title => 'test abc'})
128
- a.to_param.should eql("mensfeld-test-abc")
128
+ a.to_seo.should eql("mensfeld-test-abc")
129
129
  a.seo_url.should eql("mensfeld-test-abc")
130
130
  end
131
131
  end
@@ -135,7 +135,7 @@ describe BestElement do
135
135
  context "but surname and title exists" do
136
136
  it "should return nice looking url " do
137
137
  a = subject.create({:surname => 'mensfeld', :title => 'test abc'})
138
- a.to_param.should eql("mensfeld-test-abc")
138
+ a.to_seo.should eql("mensfeld-test-abc")
139
139
  a.reload
140
140
  a.seo_url.should eql("mensfeld-test-abc")
141
141
  end
@@ -144,7 +144,7 @@ describe BestElement do
144
144
  context "and no other param" do
145
145
  it "should return only id" do
146
146
  a = subject.create()
147
- a.to_param.should eql("#{a.id}")
147
+ a.to_seo.should eql("#{a.id}")
148
148
  a.reload
149
149
  a.seo_url.should eql("#{a.id}")
150
150
  end
@@ -154,7 +154,7 @@ describe BestElement do
154
154
  context "when there are all the params" do
155
155
  it "should return nice url" do
156
156
  a = subject.create({:name => 'maciej', :surname => 'mensfeld', :title => 'test abc'})
157
- a.to_param.should eql("maciej-mensfeld-test-abc")
157
+ a.to_seo.should eql("maciej-mensfeld-test-abc")
158
158
  a.reload
159
159
  a.seo_url.should eql("maciej-mensfeld-test-abc")
160
160
  end
@@ -163,20 +163,19 @@ describe BestElement do
163
163
  context "when we have a class which has use_id => false" do
164
164
  it "should find element only when seo_url is same (or by id)" do
165
165
  a = subject.create(:name => 'bla bla bla')
166
- subject.find(a.id).should eql(a)
167
- subject.find(a.to_param).should eql(a)
168
- lambda { subject.find("#{a.to_param}aaa") }.should raise_error(ActiveRecord::RecordNotFound)
166
+ subject.find_by_seo(a.to_seo).should eql(a)
167
+ lambda { subject.find_by_seo("#{a.to_param}aaa") }.should raise_error(ActiveRecord::RecordNotFound)
169
168
  end
170
169
  end
171
170
 
172
171
  context "when updating object" do
173
172
  it "should have refreshed seo_url" do
174
173
  a = subject.create({:name => 'mensfeld', :title => 'test abc'})
175
- a.to_param.should eql("mensfeld-test-abc")
174
+ a.to_seo.should eql("mensfeld-test-abc")
176
175
  a.name = 'kowalski'
177
176
  a.save
178
- a.to_param.should eql("kowalski-test-abc")
179
- subject.find("kowalski-test-abc").should eql(a)
177
+ a.to_seo.should eql("kowalski-test-abc")
178
+ subject.find_by_seo("kowalski-test-abc").should eql(a)
180
179
  end
181
180
  end
182
181
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_more_seo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &14799600 !ruby/object:Gem::Requirement
16
+ requirement: &13506420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *14799600
24
+ version_requirements: *13506420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: active_record
27
- requirement: &14799120 !ruby/object:Gem::Requirement
27
+ requirement: &13505960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14799120
35
+ version_requirements: *13505960
36
36
  description: Gem makes your ActiveRecord models more SEO friendly. Changes URL to
37
37
  look way better
38
38
  email: maciej@mensfeld.pl