has_seo_fields 0.0.3 → 0.0.4
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/lib/has_seo_fields.rb +19 -11
- data/lib/has_seo_fields/version.rb +1 -1
- data/spec/lib/has_seo_fields_spec.rb +23 -6
- data/spec/support/models.rb +1 -1
- metadata +4 -4
data/lib/has_seo_fields.rb
CHANGED
@@ -9,25 +9,33 @@ module HasSeoFields
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def has_seo_fields
|
12
|
+
def has_seo_fields options
|
13
|
+
@@seo_names = {}
|
13
14
|
|
14
|
-
|
15
|
-
getter_name = "seo_#{
|
15
|
+
options.each do |key, v|
|
16
|
+
getter_name = :"seo_#{key}"
|
16
17
|
|
18
|
+
# @city.seo_h1_tag
|
17
19
|
define_method getter_name do
|
18
|
-
|
20
|
+
if field = seo_fields.find_by_name(getter_name)
|
21
|
+
field.value
|
22
|
+
else
|
23
|
+
@@seo_names[getter_name]
|
24
|
+
end
|
19
25
|
end
|
20
26
|
|
27
|
+
# @city.seo_h1_tag=
|
21
28
|
define_method "#{getter_name}=" do |value|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
29
|
+
field = seo_fields.find_or_initialize_by_name getter_name
|
30
|
+
return field.destroy if value.empty? || value == @@seo_names[getter_name]
|
31
|
+
|
32
|
+
field.update_attribute(:value, value)
|
27
33
|
end
|
28
|
-
|
34
|
+
|
35
|
+
@@seo_names[getter_name] = v
|
29
36
|
end
|
30
|
-
|
37
|
+
|
38
|
+
const_set "SEO_NAMES", @@seo_names
|
31
39
|
end
|
32
40
|
end
|
33
41
|
end
|
@@ -9,7 +9,7 @@ describe HasSeoFields do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "sets SEO_NAMES" do
|
12
|
-
subject.class::SEO_NAMES.should eql(
|
12
|
+
subject.class::SEO_NAMES.should eql({ :seo_h1_tag => 'It is your city!', :seo_title => 'Clubs, bars, etc.' })
|
13
13
|
end
|
14
14
|
|
15
15
|
context "getters" do
|
@@ -19,8 +19,8 @@ describe HasSeoFields do
|
|
19
19
|
subject.seo_h1_tag.should eql('Cool!')
|
20
20
|
end
|
21
21
|
|
22
|
-
it "returns
|
23
|
-
subject.seo_title.should
|
22
|
+
it "returns the default value if nothing founded" do
|
23
|
+
subject.seo_title.should eql('Clubs, bars, etc.')
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -38,9 +38,26 @@ describe HasSeoFields do
|
|
38
38
|
subject.seo_fields.find_by_name('seo_h1_tag').value.should eql('Cool city')
|
39
39
|
end
|
40
40
|
|
41
|
-
it "does
|
42
|
-
subject.
|
43
|
-
subject.seo_fields.should have(0).
|
41
|
+
it "does note creates seo field if given value equals the default one" do
|
42
|
+
subject.seo_title = 'Clubs, bars, etc.'
|
43
|
+
subject.seo_fields.should have(0).item
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "when empty string given" do
|
47
|
+
|
48
|
+
it "does not creates entries with empty strings as values" do
|
49
|
+
subject.seo_h1_tag = ''
|
50
|
+
subject.seo_fields.should have(0).items
|
51
|
+
end
|
52
|
+
|
53
|
+
it "removes existing record if present" do
|
54
|
+
subject.seo_fields.create :name => 'seo_h1_tag', :value => 'Cool!'
|
55
|
+
seo_title = subject.seo_fields.create :name => 'seo_title', :value => 'Best city ever!'
|
56
|
+
subject.seo_h1_tag = ''
|
57
|
+
subject.reload
|
58
|
+
|
59
|
+
subject.seo_fields.should eql([seo_title])
|
60
|
+
end
|
44
61
|
end
|
45
62
|
end
|
46
63
|
|
data/spec/support/models.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_seo_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nick Erlikh
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activerecord
|