stringex 2.0.1 → 2.0.2
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 +8 -8
- data/README.rdoc +4 -5
- data/VERSION +1 -1
- data/lib/stringex/acts_as_url/adapter/base.rb +9 -4
- data/lib/stringex/acts_as_url/adapter/data_mapper.rb +4 -0
- data/lib/stringex/acts_as_url/adapter/mongoid.rb +3 -2
- data/lib/stringex/unidecoder.rb +3 -1
- data/stringex.gemspec +1 -1
- data/test/acts_as_url/adapter/active_record.rb +2 -2
- data/test/acts_as_url/adapter/data_mapper.rb +11 -9
- data/test/acts_as_url/adapter/mongoid.rb +9 -7
- data/test/acts_as_url_integration_test.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Mjk4ZWZjOTA4ODlmMDVmOWQ0YzFkNTc4ZWRiNTkzMzY5MTU3OTVkNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzcwNWY5M2YxYWY3OTZiMDJkOTIyMGM2MDVkNjRkOTU4Y2FjNmU2NQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzVkMGNjNTVjMWY1NTk3YTdkYjRkNzdkNjg1Y2U0ZWRkOTJhZDZiYjliZmYx
|
10
|
+
MGU5YTAwZDVkOTIwMWY4NDZjMWQxYzZiMDEwMDY0NDg3ZGI0NmMyMjg4YjE5
|
11
|
+
ZjE3M2Y0MWU5OTJlYzMwMDY4ZmFmMGNiMTIzM2QxOGVlNDlkMzM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzI1YzdjMWMwOGE2OGQ2MWExNzZhYWFmNjVmZTFkOTg1NmI3NTQyMzEzNGIz
|
14
|
+
NmU1ZGM4YmQ4YjZiZDRjZGE0Mzg0OGIxMDUyOTgwOGEyZjEzZGRlOThkNGU1
|
15
|
+
YjYzNmUxMzJkNDFiNDUwZDZjM2Q4YmQwOGYzMzA1OTlkYTZiNTA=
|
data/README.rdoc
CHANGED
@@ -27,7 +27,6 @@ which will populate the <tt>url</tt> attribute on the object with the converted
|
|
27
27
|
be guaranteed to lookup the expected record based on a duplicate slug.</em>
|
28
28
|
<tt>:limit</tt>:: If set, will limit length of url generated. Default is nil.
|
29
29
|
|
30
|
-
|
31
30
|
In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model:
|
32
31
|
|
33
32
|
# Inside your model
|
@@ -37,7 +36,7 @@ In order to use the generated url attribute, you will probably want to override
|
|
37
36
|
|
38
37
|
Routing called via named routes like <tt>foo_path(@foo)</tt> will automatically use the url. In your controllers you will need to call <tt>Foo.find_by_url(params[:id])</tt> instead of the regular find. Don't look for <tt>params[:url]</tt> unless you set it explicitly in the routing, <tt>to_param</tt> will generate <tt>params[:id]</tt>.
|
39
38
|
|
40
|
-
Note that if you add <tt>acts_as_url</tt> to an
|
39
|
+
Note that if you add <tt>acts_as_url</tt> to an existing model, the <tt>url</tt> database column will initially be blank. To set this column for your existing instances, you can use the <tt>initialize_urls</tt> method. So if your class is <tt>Post</tt>, just say <tt>Post.initialize_urls</tt>.
|
41
40
|
|
42
41
|
Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples:
|
43
42
|
|
@@ -90,17 +89,17 @@ In both the YAML and pure Hash implementation, the end result should be a Hash w
|
|
90
89
|
|
91
90
|
== StringExtensions
|
92
91
|
|
93
|
-
A collection of extensions on Ruby's String class. Please see the documentation for StringExtensions module for more information. There's not much to explain about them really.
|
92
|
+
A small collection of extensions on Ruby's String class. Please see the documentation for StringExtensions module for more information. There's not much to explain about them really.
|
94
93
|
|
95
94
|
== Localization
|
96
95
|
|
97
|
-
With Stringex version 2.0 and higher, you can localize the different conversions in Stringex. Read more here[https://github.com/rsl/stringex/wiki/Localization-of-Stringex-conversions]
|
96
|
+
With Stringex version 2.0 and higher, you can localize the different conversions in Stringex. Read more here[https://github.com/rsl/stringex/wiki/Localization-of-Stringex-conversions]. If you add a new language, please submit a pull request so we can make it available to other users also.
|
98
97
|
|
99
98
|
== Using with Rails
|
100
99
|
|
101
100
|
When using Stringex with Ruby on Rails, you automatically get built-in translations for miscellaneous characters, HTML entities, and vulgar fractions.
|
102
101
|
|
103
|
-
You can see Stringex's standard translations here
|
102
|
+
You can see Stringex's standard translations here[https://github.com/rsl/stringex/tree/master/locales].
|
104
103
|
|
105
104
|
Currently, built-in translations are available for the following languages:
|
106
105
|
English (en), Danish (da)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.0.
|
1
|
+
2.0.2
|
@@ -56,14 +56,15 @@ module Stringex
|
|
56
56
|
|
57
57
|
def add_new_record_url_owner_conditions
|
58
58
|
return if is_new?(instance)
|
59
|
-
@url_owner_conditions.first << " and #{
|
59
|
+
@url_owner_conditions.first << " and #{primary_key} != ?"
|
60
60
|
@url_owner_conditions << instance.id
|
61
61
|
end
|
62
62
|
|
63
63
|
def add_scoped_url_owner_conditions
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
[settings.scope_for_url].flatten.compact.each do |scope|
|
65
|
+
@url_owner_conditions.first << " and #{scope} = ?"
|
66
|
+
@url_owner_conditions << instance.send(scope)
|
67
|
+
end
|
67
68
|
end
|
68
69
|
|
69
70
|
def create_callback
|
@@ -145,6 +146,10 @@ module Stringex
|
|
145
146
|
self.class.orm_class
|
146
147
|
end
|
147
148
|
|
149
|
+
def primary_key
|
150
|
+
instance.class.primary_key
|
151
|
+
end
|
152
|
+
|
148
153
|
def read_attribute(instance, attribute)
|
149
154
|
instance.read_attribute attribute
|
150
155
|
end
|
@@ -42,6 +42,10 @@ module Stringex
|
|
42
42
|
klass.all(:conditions => {settings.url_attribute => nil}).each(&block)
|
43
43
|
end
|
44
44
|
|
45
|
+
def primary_key
|
46
|
+
instance.class.key.first.instance_variable_get '@name'
|
47
|
+
end
|
48
|
+
|
45
49
|
def read_attribute(instance, attribute)
|
46
50
|
instance.attributes[attribute]
|
47
51
|
end
|
@@ -15,8 +15,9 @@ module Stringex
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def add_scoped_url_owner_conditions
|
18
|
-
|
19
|
-
|
18
|
+
[settings.scope_for_url].flatten.compact.each do |scope|
|
19
|
+
@url_owner_conditions.merge! scope => instance.send(scope)
|
20
|
+
end
|
20
21
|
end
|
21
22
|
|
22
23
|
def get_base_url_owner_conditions
|
data/lib/stringex/unidecoder.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'yaml'
|
4
|
+
|
3
5
|
module Stringex
|
4
6
|
module Unidecoder
|
5
7
|
# Contains Unicode codepoints, loading as needed from YAML files
|
6
8
|
CODEPOINTS = Hash.new{|h, k|
|
7
|
-
h[k] = YAML.load_file(File.join(File.expand_path(File.dirname(__FILE__)), "unidecoder_data", "#{k}.yml"))
|
9
|
+
h[k] = ::YAML.load_file(File.join(File.expand_path(File.dirname(__FILE__)), "unidecoder_data", "#{k}.yml"))
|
8
10
|
} unless defined?(CODEPOINTS)
|
9
11
|
|
10
12
|
class << self
|
data/stringex.gemspec
CHANGED
@@ -14,11 +14,11 @@ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":me
|
|
14
14
|
ActiveRecord::Migration.verbose = false
|
15
15
|
ActiveRecord::Schema.define do
|
16
16
|
create_table :documents, :force => true do |t|
|
17
|
-
t.string :title, :other, :url
|
17
|
+
t.string :title, :other, :another, :url
|
18
18
|
end
|
19
19
|
|
20
20
|
create_table :sti_base_documents, :force => true do |t|
|
21
|
-
t.string :title, :other, :url, :type
|
21
|
+
t.string :title, :other, :another, :url, :type
|
22
22
|
end
|
23
23
|
end
|
24
24
|
ActiveRecord::Migration.verbose = true
|
@@ -22,21 +22,23 @@ DataMapper.setup :default, 'sqlite::memory:'
|
|
22
22
|
DefineTestClasses = proc do
|
23
23
|
class Document
|
24
24
|
include DataMapper::Resource
|
25
|
-
property :id,
|
26
|
-
property :title,
|
27
|
-
property :other,
|
28
|
-
property :
|
25
|
+
property :id, Serial
|
26
|
+
property :title, String
|
27
|
+
property :other, String
|
28
|
+
property :another, String
|
29
|
+
property :url, String, :lazy => false
|
29
30
|
|
30
31
|
acts_as_url :title
|
31
32
|
end
|
32
33
|
|
33
34
|
class STIBaseDocument
|
34
35
|
include DataMapper::Resource
|
35
|
-
property :id,
|
36
|
-
property :title,
|
37
|
-
property :other,
|
38
|
-
property :
|
39
|
-
property :
|
36
|
+
property :id, Serial
|
37
|
+
property :title, String
|
38
|
+
property :other, String
|
39
|
+
property :another, String
|
40
|
+
property :url, String, :lazy => false
|
41
|
+
property :type, String
|
40
42
|
|
41
43
|
# This gets redefined in the only test that uses it but I want to be uniform
|
42
44
|
# in setting configuration details in the tests themselves
|
@@ -15,19 +15,21 @@ end
|
|
15
15
|
|
16
16
|
class Document
|
17
17
|
include Mongoid::Document
|
18
|
-
field :title,
|
19
|
-
field :other,
|
20
|
-
field :
|
18
|
+
field :title, :type => String
|
19
|
+
field :other, :type => String
|
20
|
+
field :another, :type => String
|
21
|
+
field :url, :type => String
|
21
22
|
|
22
23
|
acts_as_url :title
|
23
24
|
end
|
24
25
|
|
25
26
|
class STIBaseDocument
|
26
27
|
include Mongoid::Document
|
27
|
-
field :title,
|
28
|
-
field :other,
|
29
|
-
field :
|
30
|
-
field :
|
28
|
+
field :title, :type => String
|
29
|
+
field :other, :type => String
|
30
|
+
field :another, :type => String
|
31
|
+
field :url, :type => String
|
32
|
+
field :type, :type => String
|
31
33
|
|
32
34
|
# This gets redefined in the only test that uses it but I want to be uniform
|
33
35
|
# in setting configuration details in the tests themselves
|
@@ -89,6 +89,29 @@ class ActsAsUrlIntegrationTest < Test::Unit::TestCase
|
|
89
89
|
assert_not_equal @doc.url, @other_doc.url
|
90
90
|
end
|
91
91
|
|
92
|
+
def test_should_allow_multiple_scopes
|
93
|
+
Document.class_eval do
|
94
|
+
acts_as_url :title, :scope => [:other, :another]
|
95
|
+
end
|
96
|
+
|
97
|
+
@doc = Document.create(:title => "Mocumentary", :other => "I don't care if I'm unique for some reason",
|
98
|
+
:another => "Whatever")
|
99
|
+
@other_doc = Document.create(:title => "Mocumentary", :other => "Me either", :another => "Whatever")
|
100
|
+
assert_equal @doc.url, @other_doc.url
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_only_create_unique_urls_for_multiple_scopes_if_both_attributes_are_same
|
104
|
+
Document.class_eval do
|
105
|
+
acts_as_url :title, :scope => [:other, :another]
|
106
|
+
end
|
107
|
+
|
108
|
+
@doc = Document.create(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique",
|
109
|
+
:another => "Whatever")
|
110
|
+
@other_doc = Document.create(:title => "Mocumentary", :other => "Suddenly, I care if I'm unique",
|
111
|
+
:another => "Whatever")
|
112
|
+
assert_not_equal @doc.url, @other_doc.url
|
113
|
+
end
|
114
|
+
|
92
115
|
def test_should_allow_setting_url_attribute
|
93
116
|
Document.class_eval do
|
94
117
|
# Manually undefining the url method on Document which, in a real class not reused for tests,
|