ardb 0.27.0 → 0.27.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ardb.rb +1 -0
- data/lib/ardb/has_slug.rb +30 -31
- data/lib/ardb/require_autoloaded_active_record_files.rb +69 -0
- data/lib/ardb/version.rb +1 -1
- data/script/determine_autoloaded_active_record_files.rb +91 -0
- data/test/unit/has_slug_tests.rb +100 -87
- metadata +32 -74
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
data.tar.gz: bd6e2e2855ebc107bc3da24642e8a996da2fa06b
|
4
|
+
metadata.gz: fb550afe9e0c76af643938e94fb4fcab9fe1afeb
|
5
|
+
SHA512:
|
6
|
+
data.tar.gz: bdd10e94e00e33f2800be95666efed91518da00f3109e73e1878e341fa6ad5a384f5a7d78f5ebaa07b7ed136bd6b122fdd29dea43c2242c746986a71d9fb092e
|
7
|
+
metadata.gz: 51e6669ff3c303a14bcec3e5eb15c0dbb7442216257a1479421588de0a4e675eb9dc54bd068d9519b9dc1bbbccbf8756cba59d563529fa6dff6ad71d2debccd1
|
data/lib/ardb.rb
CHANGED
data/lib/ardb/has_slug.rb
CHANGED
@@ -13,8 +13,7 @@ module Ardb
|
|
13
13
|
extend ClassMethods
|
14
14
|
include InstanceMethods
|
15
15
|
|
16
|
-
@
|
17
|
-
|
16
|
+
@ardb_has_slug_configs = Hash.new{ |h, k| h[k] = {} }
|
18
17
|
end
|
19
18
|
|
20
19
|
module ClassMethods
|
@@ -23,8 +22,8 @@ module Ardb
|
|
23
22
|
options ||= {}
|
24
23
|
raise(ArgumentError, "a source must be provided") unless options[:source]
|
25
24
|
|
26
|
-
|
27
|
-
|
25
|
+
attribute = (options[:attribute] || DEFAULT_ATTRIBUTE).to_sym
|
26
|
+
@ardb_has_slug_configs[attribute].merge!({
|
28
27
|
:source_proc => options[:source].to_proc,
|
29
28
|
:preprocessor_proc => (options[:preprocessor] || DEFAULT_PREPROCESSOR).to_proc,
|
30
29
|
:separator => options[:separator] || DEFAULT_SEPARATOR,
|
@@ -33,18 +32,18 @@ module Ardb
|
|
33
32
|
|
34
33
|
# since the slug isn't written till an after callback we can't always
|
35
34
|
# validate presence of it
|
36
|
-
validates_presence_of(
|
37
|
-
validates_uniqueness_of(
|
35
|
+
validates_presence_of(attribute, :on => :update)
|
36
|
+
validates_uniqueness_of(attribute, {
|
38
37
|
:case_sensitive => true,
|
39
38
|
:scope => options[:unique_scope]
|
40
39
|
})
|
41
40
|
|
42
|
-
after_create :
|
43
|
-
after_update :
|
41
|
+
after_create :ardb_has_slug_generate_slugs
|
42
|
+
after_update :ardb_has_slug_generate_slugs
|
44
43
|
end
|
45
44
|
|
46
|
-
def
|
47
|
-
@
|
45
|
+
def ardb_has_slug_configs
|
46
|
+
@ardb_has_slug_configs
|
48
47
|
end
|
49
48
|
|
50
49
|
end
|
@@ -53,37 +52,37 @@ module Ardb
|
|
53
52
|
|
54
53
|
private
|
55
54
|
|
56
|
-
def reset_slug
|
57
|
-
|
55
|
+
def reset_slug(attribute = nil)
|
56
|
+
attribute ||= DEFAULT_ATTRIBUTE
|
57
|
+
self.send("#{attribute}=", nil)
|
58
58
|
end
|
59
59
|
|
60
|
-
def
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
def ardb_has_slug_generate_slugs
|
61
|
+
self.class.ardb_has_slug_configs.each do |attr_name, config|
|
62
|
+
slug_source = if !self.send(attr_name) || self.send(attr_name).to_s.empty?
|
63
|
+
self.instance_eval(&config[:source_proc])
|
64
|
+
else
|
65
|
+
self.send(attr_name)
|
66
|
+
end
|
67
|
+
|
68
|
+
generated_slug = Slug.new(slug_source, {
|
69
|
+
:preprocessor => config[:preprocessor_proc],
|
70
|
+
:separator => config[:separator],
|
71
|
+
:allow_underscores => config[:allow_underscores]
|
72
|
+
})
|
73
|
+
next if self.send(attr_name) == generated_slug
|
74
|
+
self.send("#{attr_name}=", generated_slug)
|
75
|
+
self.update_column(attr_name, generated_slug)
|
66
76
|
end
|
67
|
-
|
68
|
-
generated_slug = Slug.new(slug_source, {
|
69
|
-
:preprocessor => self.class.ardb_has_slug_config[:preprocessor_proc],
|
70
|
-
:separator => self.class.ardb_has_slug_config[:separator],
|
71
|
-
:allow_underscores => self.class.ardb_has_slug_config[:allow_underscores]
|
72
|
-
})
|
73
|
-
return if self.send(attr_name) == generated_slug
|
74
|
-
self.send("#{attr_name}=", generated_slug)
|
75
|
-
self.update_column(attr_name, generated_slug)
|
76
77
|
end
|
77
78
|
|
78
79
|
end
|
79
80
|
|
80
81
|
module Slug
|
81
|
-
DEFAULT_PREPROCESSOR = proc{ |slug| slug } # no-op
|
82
|
-
|
83
82
|
def self.new(string, options = nil)
|
84
83
|
options ||= {}
|
85
|
-
preprocessor = options[:preprocessor]
|
86
|
-
separator = options[:separator]
|
84
|
+
preprocessor = options[:preprocessor]
|
85
|
+
separator = options[:separator]
|
87
86
|
allow_underscores = options[:allow_underscores]
|
88
87
|
regexp_escaped_sep = Regexp.escape(separator)
|
89
88
|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# ActiveRecord makes use of autoload to load some of its components as-needed.
|
2
|
+
# This doesn't work well with threaded environments, and causes uninitialized
|
3
|
+
# constants. To avoid this, this file manually requires the following files that
|
4
|
+
# are not required using `require 'active_record'`. Trying to automatically
|
5
|
+
# require every file in ActiveRecord is slow and inefficient. Many of the files
|
6
|
+
# fail to require and there are some we don't want to require. Thus, this is a
|
7
|
+
# manual list of requires.
|
8
|
+
|
9
|
+
# To re-build this list of requires, run the following:
|
10
|
+
# bundle exec ruby script/determine_autoloaded_active_record_files.rb
|
11
|
+
|
12
|
+
# For compatibility, we require active record first
|
13
|
+
require 'active_record'
|
14
|
+
|
15
|
+
require 'active_record/aggregations'
|
16
|
+
require 'active_record/associations'
|
17
|
+
require 'active_record/associations/alias_tracker'
|
18
|
+
require 'active_record/associations/association'
|
19
|
+
require 'active_record/associations/association_scope'
|
20
|
+
require 'active_record/associations/belongs_to_association'
|
21
|
+
require 'active_record/associations/belongs_to_polymorphic_association'
|
22
|
+
require 'active_record/associations/builder/association'
|
23
|
+
require 'active_record/associations/builder/belongs_to'
|
24
|
+
require 'active_record/associations/builder/collection_association'
|
25
|
+
require 'active_record/associations/builder/has_and_belongs_to_many'
|
26
|
+
require 'active_record/associations/builder/has_many'
|
27
|
+
require 'active_record/associations/builder/has_one'
|
28
|
+
require 'active_record/associations/collection_association'
|
29
|
+
require 'active_record/associations/collection_proxy'
|
30
|
+
require 'active_record/associations/has_and_belongs_to_many_association'
|
31
|
+
require 'active_record/associations/has_many_association'
|
32
|
+
require 'active_record/associations/has_many_through_association'
|
33
|
+
require 'active_record/associations/has_one_association'
|
34
|
+
require 'active_record/associations/has_one_through_association'
|
35
|
+
require 'active_record/associations/join_dependency'
|
36
|
+
require 'active_record/associations/join_dependency/join_association'
|
37
|
+
require 'active_record/associations/join_dependency/join_base'
|
38
|
+
require 'active_record/associations/preloader'
|
39
|
+
require 'active_record/associations/preloader/association'
|
40
|
+
require 'active_record/associations/preloader/belongs_to'
|
41
|
+
require 'active_record/associations/preloader/collection_association'
|
42
|
+
require 'active_record/associations/preloader/has_and_belongs_to_many'
|
43
|
+
require 'active_record/associations/preloader/has_many'
|
44
|
+
require 'active_record/associations/preloader/has_many_through'
|
45
|
+
require 'active_record/associations/preloader/has_one'
|
46
|
+
require 'active_record/associations/preloader/has_one_through'
|
47
|
+
require 'active_record/attribute_assignment'
|
48
|
+
require 'active_record/attribute_methods/before_type_cast'
|
49
|
+
require 'active_record/attribute_methods/deprecated_underscore_read'
|
50
|
+
require 'active_record/attribute_methods/dirty'
|
51
|
+
require 'active_record/attribute_methods/primary_key'
|
52
|
+
require 'active_record/attribute_methods/query'
|
53
|
+
require 'active_record/attribute_methods/read'
|
54
|
+
require 'active_record/attribute_methods/serialization'
|
55
|
+
require 'active_record/attribute_methods/time_zone_conversion'
|
56
|
+
require 'active_record/autosave_association'
|
57
|
+
require 'active_record/base'
|
58
|
+
require 'active_record/dynamic_finder_match'
|
59
|
+
require 'active_record/dynamic_scope_match'
|
60
|
+
require 'active_record/observer'
|
61
|
+
require 'active_record/relation'
|
62
|
+
require 'active_record/relation/predicate_builder'
|
63
|
+
require 'active_record/result'
|
64
|
+
|
65
|
+
# There are also issues with requiring ActiveSupport. This doesn't require every
|
66
|
+
# ActiveSupport file though, only ones we've seen cause problems.
|
67
|
+
require 'active_support'
|
68
|
+
|
69
|
+
require 'active_support/multibyte/chars'
|
data/lib/ardb/version.rb
CHANGED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
# this can be slow, this is one of the reasons this shouldn't be done during
|
4
|
+
# the startup of our apps
|
5
|
+
gemspec = Gem.loaded_specs['activerecord']
|
6
|
+
|
7
|
+
puts "Looking at files in: "
|
8
|
+
puts " #{gemspec.lib_dirs_glob.inspect}"
|
9
|
+
|
10
|
+
paths = Dir["#{gemspec.lib_dirs_glob}/**/*.rb"]
|
11
|
+
|
12
|
+
# these are regexs for files we want to ignore requiring. for example,
|
13
|
+
# generators fail when we try to require them. the others are pieces of active
|
14
|
+
# record we don't use in a production environment
|
15
|
+
ignored_regexes = [
|
16
|
+
/rails\/generators/,
|
17
|
+
/active_record\/railtie/,
|
18
|
+
/active_record\/migration/,
|
19
|
+
/active_record\/fixtures/,
|
20
|
+
/active_record\/schema/,
|
21
|
+
/active_record\/connection_adapters/,
|
22
|
+
/active_record\/test_case/,
|
23
|
+
/active_record\/coders\/yaml_column/
|
24
|
+
]
|
25
|
+
|
26
|
+
Result = Struct.new(:file, :state, :reason)
|
27
|
+
|
28
|
+
already_required = []
|
29
|
+
needs_to_be_required = []
|
30
|
+
ignored = []
|
31
|
+
errored = []
|
32
|
+
|
33
|
+
paths.sort.each do |full_path|
|
34
|
+
relative_path_with_rb = full_path.gsub("#{gemspec.lib_dirs_glob}/", '')
|
35
|
+
relative_path = relative_path_with_rb.gsub(/\.rb\z/, '')
|
36
|
+
|
37
|
+
result = Result.new(relative_path)
|
38
|
+
|
39
|
+
# see if it's ignored
|
40
|
+
ignored_regexes.each do |regex|
|
41
|
+
if relative_path =~ regex
|
42
|
+
result.state = :ignored
|
43
|
+
result.reason = "matched #{regex}"
|
44
|
+
break
|
45
|
+
end
|
46
|
+
end
|
47
|
+
if result.state == :ignored
|
48
|
+
ignored << result
|
49
|
+
next
|
50
|
+
end
|
51
|
+
|
52
|
+
# try requiring the file
|
53
|
+
begin
|
54
|
+
if result.state = require(relative_path)
|
55
|
+
needs_to_be_required << result
|
56
|
+
else
|
57
|
+
already_required << result
|
58
|
+
end
|
59
|
+
rescue LoadError, SyntaxError => exception
|
60
|
+
result.state = :errored
|
61
|
+
result.reason = "#{exception.class}: #{exception.message}"
|
62
|
+
errored << result
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
puts "Results\n"
|
67
|
+
|
68
|
+
puts "Ignored:"
|
69
|
+
ignored.each do |result|
|
70
|
+
puts " #{result.file}"
|
71
|
+
puts " #{result.reason}"
|
72
|
+
end
|
73
|
+
puts "\n"
|
74
|
+
|
75
|
+
puts "Errored:"
|
76
|
+
errored.each do |result|
|
77
|
+
puts " #{result.file}"
|
78
|
+
puts " #{result.reason}"
|
79
|
+
end
|
80
|
+
puts "\n"
|
81
|
+
|
82
|
+
puts "Already Required:"
|
83
|
+
already_required.each do |result|
|
84
|
+
puts " #{result.file}"
|
85
|
+
end
|
86
|
+
puts "\n"
|
87
|
+
|
88
|
+
puts "Needs To Be Required:\n"
|
89
|
+
needs_to_be_required.each do |result|
|
90
|
+
puts "require '#{result.file}'"
|
91
|
+
end
|
data/test/unit/has_slug_tests.rb
CHANGED
@@ -13,12 +13,12 @@ module Ardb::HasSlug
|
|
13
13
|
slug_attribute = @slug_attribute = Factory.string.to_sym
|
14
14
|
@record_class = Ardb::RecordSpy.new do
|
15
15
|
include Ardb::HasSlug
|
16
|
-
attr_accessor source_attribute, slug_attribute
|
17
|
-
attr_reader :
|
16
|
+
attr_accessor source_attribute, slug_attribute, DEFAULT_ATTRIBUTE
|
17
|
+
attr_reader :slug_db_column_updates
|
18
18
|
|
19
|
-
def update_column(
|
20
|
-
@
|
21
|
-
@
|
19
|
+
def update_column(*args)
|
20
|
+
@slug_db_column_updates ||= []
|
21
|
+
@slug_db_column_updates << args
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -28,10 +28,10 @@ module Ardb::HasSlug
|
|
28
28
|
('{'..'~').to_a - ['-', '_']).freeze
|
29
29
|
|
30
30
|
should have_imeths :has_slug
|
31
|
-
should have_imeths :
|
31
|
+
should have_imeths :ardb_has_slug_configs
|
32
32
|
|
33
33
|
should "use much-plugin" do
|
34
|
-
assert_includes MuchPlugin, Ardb::
|
34
|
+
assert_includes MuchPlugin, Ardb::HasSlug
|
35
35
|
end
|
36
36
|
|
37
37
|
should "know its default attribute, preprocessor and separator" do
|
@@ -40,8 +40,8 @@ module Ardb::HasSlug
|
|
40
40
|
assert_equal '-', DEFAULT_SEPARATOR
|
41
41
|
end
|
42
42
|
|
43
|
-
should "not have any has-slug
|
44
|
-
assert_equal({}, subject.
|
43
|
+
should "not have any has-slug configs by default" do
|
44
|
+
assert_equal({}, subject.ardb_has_slug_configs)
|
45
45
|
end
|
46
46
|
|
47
47
|
should "default the has slug config using `has_slug`" do
|
@@ -49,17 +49,17 @@ module Ardb::HasSlug
|
|
49
49
|
string = Factory.string
|
50
50
|
record = subject.new.tap{ |r| r.send("#{@source_attribute}=", string) }
|
51
51
|
|
52
|
-
|
53
|
-
assert_equal DEFAULT_SEPARATOR,
|
54
|
-
assert_false
|
52
|
+
config = subject.ardb_has_slug_configs[DEFAULT_ATTRIBUTE]
|
53
|
+
assert_equal DEFAULT_SEPARATOR, config[:separator]
|
54
|
+
assert_false config[:allow_underscores]
|
55
55
|
|
56
|
-
source_proc =
|
56
|
+
source_proc = config[:source_proc]
|
57
57
|
assert_instance_of Proc, source_proc
|
58
58
|
exp = record.send(@source_attribute)
|
59
59
|
assert_equal exp, record.instance_eval(&source_proc)
|
60
60
|
|
61
61
|
upcase_string = string.upcase
|
62
|
-
preprocessor_proc =
|
62
|
+
preprocessor_proc = config[:preprocessor_proc]
|
63
63
|
assert_instance_of Proc, preprocessor_proc
|
64
64
|
exp = upcase_string.send(DEFAULT_PREPROCESSOR)
|
65
65
|
assert_equal exp, preprocessor_proc.call(upcase_string)
|
@@ -76,27 +76,28 @@ module Ardb::HasSlug
|
|
76
76
|
:allow_underscores => allow_underscore
|
77
77
|
})
|
78
78
|
|
79
|
-
|
80
|
-
assert_equal separator,
|
81
|
-
assert_equal allow_underscore,
|
79
|
+
config = subject.ardb_has_slug_configs[@slug_attribute]
|
80
|
+
assert_equal separator, config[:separator]
|
81
|
+
assert_equal allow_underscore, config[:allow_underscores]
|
82
82
|
|
83
83
|
value = Factory.string.downcase
|
84
|
-
preprocessor_proc =
|
84
|
+
preprocessor_proc = config[:preprocessor_proc]
|
85
85
|
assert_instance_of Proc, preprocessor_proc
|
86
86
|
assert_equal value.upcase, preprocessor_proc.call(value)
|
87
87
|
end
|
88
88
|
|
89
89
|
should "add validations using `has_slug`" do
|
90
90
|
subject.has_slug :source => @source_attribute
|
91
|
+
exp_attr_name = DEFAULT_ATTRIBUTE
|
91
92
|
|
92
93
|
validation = subject.validations.find{ |v| v.type == :presence }
|
93
94
|
assert_not_nil validation
|
94
|
-
assert_equal [
|
95
|
+
assert_equal [exp_attr_name], validation.columns
|
95
96
|
assert_equal :update, validation.options[:on]
|
96
97
|
|
97
98
|
validation = subject.validations.find{ |v| v.type == :uniqueness }
|
98
99
|
assert_not_nil validation
|
99
|
-
assert_equal [
|
100
|
+
assert_equal [exp_attr_name], validation.columns
|
100
101
|
assert_equal true, validation.options[:case_sensitive]
|
101
102
|
assert_nil validation.options[:scope]
|
102
103
|
end
|
@@ -118,11 +119,11 @@ module Ardb::HasSlug
|
|
118
119
|
|
119
120
|
callback = subject.callbacks.find{ |v| v.type == :after_create }
|
120
121
|
assert_not_nil callback
|
121
|
-
assert_equal [:
|
122
|
+
assert_equal [:ardb_has_slug_generate_slugs], callback.args
|
122
123
|
|
123
124
|
callback = subject.callbacks.find{ |v| v.type == :after_update }
|
124
125
|
assert_not_nil callback
|
125
|
-
assert_equal [:
|
126
|
+
assert_equal [:ardb_has_slug_generate_slugs], callback.args
|
126
127
|
end
|
127
128
|
|
128
129
|
should "raise an argument error if `has_slug` isn't passed a source" do
|
@@ -137,6 +138,8 @@ module Ardb::HasSlug
|
|
137
138
|
@preprocessor = [:downcase, :upcase, :capitalize].choice
|
138
139
|
@separator = NON_WORD_CHARS.choice
|
139
140
|
@allow_underscores = Factory.boolean
|
141
|
+
|
142
|
+
@record_class.has_slug(:source => @source_attribute)
|
140
143
|
@record_class.has_slug({
|
141
144
|
:attribute => @slug_attribute,
|
142
145
|
:source => @source_attribute,
|
@@ -152,112 +155,121 @@ module Ardb::HasSlug
|
|
152
155
|
# generating a slug
|
153
156
|
@source_value = "#{Factory.string.downcase}_#{Factory.string.upcase}"
|
154
157
|
@record.send("#{@source_attribute}=", @source_value)
|
158
|
+
|
159
|
+
@exp_default_slug = Slug.new(@source_value, {
|
160
|
+
:preprocessor => DEFAULT_PREPROCESSOR.to_proc,
|
161
|
+
:separator => DEFAULT_SEPARATOR
|
162
|
+
})
|
163
|
+
@exp_custom_slug = Slug.new(@source_value, {
|
164
|
+
:preprocessor => @preprocessor.to_proc,
|
165
|
+
:separator => @separator,
|
166
|
+
:allow_underscores => @allow_underscores
|
167
|
+
})
|
155
168
|
end
|
156
169
|
subject{ @record }
|
157
170
|
|
158
171
|
should "reset its slug using `reset_slug`" do
|
172
|
+
# reset the default attribute
|
173
|
+
subject.send("#{DEFAULT_ATTRIBUTE}=", Factory.slug)
|
174
|
+
assert_not_nil subject.send(DEFAULT_ATTRIBUTE)
|
175
|
+
subject.instance_eval{ reset_slug }
|
176
|
+
assert_nil subject.send(DEFAULT_ATTRIBUTE)
|
177
|
+
|
178
|
+
# reset a custom attribute
|
159
179
|
subject.send("#{@slug_attribute}=", Factory.slug)
|
160
180
|
assert_not_nil subject.send(@slug_attribute)
|
161
|
-
|
181
|
+
sa = @slug_attribute
|
182
|
+
subject.instance_eval{ reset_slug(sa) }
|
162
183
|
assert_nil subject.send(@slug_attribute)
|
163
184
|
end
|
164
185
|
|
165
|
-
should "default its slug attribute
|
166
|
-
subject.instance_eval{
|
186
|
+
should "default its slug attribute" do
|
187
|
+
subject.instance_eval{ ardb_has_slug_generate_slugs }
|
188
|
+
assert_equal 2, subject.slug_db_column_updates.size
|
167
189
|
|
168
|
-
exp =
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
assert_equal exp,
|
174
|
-
|
175
|
-
|
190
|
+
exp = @exp_default_slug
|
191
|
+
assert_equal exp, subject.send(DEFAULT_ATTRIBUTE)
|
192
|
+
assert_includes [DEFAULT_ATTRIBUTE, exp], subject.slug_db_column_updates
|
193
|
+
|
194
|
+
exp = @exp_custom_slug
|
195
|
+
assert_equal exp, subject.send(@slug_attribute)
|
196
|
+
assert_includes [@slug_attribute, exp], subject.slug_db_column_updates
|
197
|
+
end
|
198
|
+
|
199
|
+
should "not set its slug if it hasn't changed" do
|
200
|
+
@record.send("#{DEFAULT_ATTRIBUTE}=", @exp_default_slug)
|
201
|
+
@record.send("#{@slug_attribute}=", @exp_custom_slug)
|
202
|
+
|
203
|
+
subject.instance_eval{ ardb_has_slug_generate_slugs }
|
204
|
+
assert_nil subject.slug_db_column_updates
|
176
205
|
end
|
177
206
|
|
178
|
-
should "slug its slug attribute value if set
|
207
|
+
should "slug its slug attribute value if set" do
|
179
208
|
@record.send("#{@slug_attribute}=", @source_value)
|
180
209
|
# change the source attr to some random value, to avoid a false positive
|
181
210
|
@record.send("#{@source_attribute}=", Factory.string)
|
182
|
-
subject.instance_eval{
|
211
|
+
subject.instance_eval{ ardb_has_slug_generate_slugs }
|
183
212
|
|
184
|
-
exp =
|
185
|
-
|
186
|
-
|
187
|
-
:allow_underscores => @allow_underscores
|
188
|
-
})
|
189
|
-
assert_equal exp, subject.send(@slug_attribute)
|
190
|
-
assert_equal @slug_attribute, subject.slug_db_column_name
|
191
|
-
assert_equal exp, subject.slug_db_column_value
|
213
|
+
exp = @exp_custom_slug
|
214
|
+
assert_equal exp, subject.send(@slug_attribute)
|
215
|
+
assert_includes [@slug_attribute, exp], subject.slug_db_column_updates
|
192
216
|
end
|
193
217
|
|
194
|
-
should "slug its source even if its already a valid slug
|
218
|
+
should "slug its source even if its already a valid slug" do
|
195
219
|
slug_source = Factory.slug
|
196
220
|
@record.send("#{@source_attribute}=", slug_source)
|
197
221
|
# ensure the preprocessor doesn't change our source
|
198
222
|
Assert.stub(slug_source, @preprocessor){ slug_source }
|
199
223
|
|
200
|
-
subject.instance_eval{
|
224
|
+
subject.instance_eval{ ardb_has_slug_generate_slugs }
|
201
225
|
|
202
226
|
exp = Slug.new(slug_source, {
|
203
227
|
:preprocessor => @preprocessor.to_proc,
|
204
228
|
:separator => @separator,
|
205
229
|
:allow_underscores => @allow_underscores
|
206
230
|
})
|
207
|
-
assert_equal exp,
|
208
|
-
|
209
|
-
assert_equal exp, subject.slug_db_column_value
|
210
|
-
end
|
211
|
-
|
212
|
-
should "not set its slug if it hasn't changed using `ardb_has_slug_generate_slug`" do
|
213
|
-
generated_slug = Slug.new(@source_value, {
|
214
|
-
:preprocessor => @preprocessor.to_proc,
|
215
|
-
:separator => @separator,
|
216
|
-
:allow_underscores => @allow_underscores
|
217
|
-
})
|
218
|
-
@record.send("#{@slug_attribute}=", generated_slug)
|
219
|
-
subject.instance_eval{ ardb_has_slug_generate_slug }
|
220
|
-
|
221
|
-
assert_nil subject.slug_db_column_name
|
222
|
-
assert_nil subject.slug_db_column_value
|
231
|
+
assert_equal exp, subject.send(@slug_attribute)
|
232
|
+
assert_includes [@slug_attribute, exp], subject.slug_db_column_updates
|
223
233
|
end
|
224
234
|
|
225
235
|
end
|
226
236
|
|
227
237
|
class SlugTests < UnitTests
|
228
238
|
desc "Slug"
|
239
|
+
setup do
|
240
|
+
@no_op_pp = proc{ |slug| slug }
|
241
|
+
@args = {
|
242
|
+
:preprocessor => @no_op_pp,
|
243
|
+
:separator => '-'
|
244
|
+
}
|
245
|
+
end
|
229
246
|
subject{ Slug }
|
230
247
|
|
231
248
|
should have_imeths :new
|
232
249
|
|
233
|
-
should "know its default preprocessor" do
|
234
|
-
assert_instance_of Proc, Slug::DEFAULT_PREPROCESSOR
|
235
|
-
string = Factory.string
|
236
|
-
assert_same string, Slug::DEFAULT_PREPROCESSOR.call(string)
|
237
|
-
end
|
238
|
-
|
239
250
|
should "not change strings that are made up of valid chars" do
|
240
251
|
string = Factory.string
|
241
|
-
assert_equal string, subject.new(string)
|
252
|
+
assert_equal string, subject.new(string, @args)
|
253
|
+
|
242
254
|
string = "#{Factory.string}-#{Factory.string.upcase}"
|
243
|
-
assert_equal string, subject.new(string)
|
255
|
+
assert_equal string, subject.new(string, @args)
|
244
256
|
end
|
245
257
|
|
246
258
|
should "turn invalid chars into a separator" do
|
247
259
|
string = Factory.integer(3).times.map do
|
248
260
|
"#{Factory.string(3)}#{NON_WORD_CHARS.choice}#{Factory.string(3)}"
|
249
261
|
end.join(NON_WORD_CHARS.choice)
|
250
|
-
assert_equal string.gsub(/[^\w]+/, '-'), subject.new(string)
|
262
|
+
assert_equal string.gsub(/[^\w]+/, '-'), subject.new(string, @args)
|
251
263
|
end
|
252
264
|
|
253
265
|
should "allow passing a custom preprocessor proc" do
|
254
266
|
string = "#{Factory.string}-#{Factory.string.upcase}"
|
255
|
-
|
256
|
-
assert_equal string.downcase
|
267
|
+
exp = string.downcase
|
268
|
+
assert_equal exp, subject.new(string, @args.merge(:preprocessor => :downcase.to_proc))
|
257
269
|
|
258
270
|
preprocessor = proc{ |s| s.gsub(/[A-Z]/, 'a') }
|
259
|
-
|
260
|
-
assert_equal
|
271
|
+
exp = preprocessor.call(string)
|
272
|
+
assert_equal exp, subject.new(string, @args.merge(:preprocessor => preprocessor))
|
261
273
|
end
|
262
274
|
|
263
275
|
should "allow passing a custom separator" do
|
@@ -265,47 +277,48 @@ module Ardb::HasSlug
|
|
265
277
|
|
266
278
|
invalid_char = (NON_WORD_CHARS - [separator]).choice
|
267
279
|
string = "#{Factory.string}#{invalid_char}#{Factory.string}"
|
268
|
-
|
269
|
-
assert_equal string.
|
280
|
+
exp = string.gsub(/[^\w]+/, separator)
|
281
|
+
assert_equal exp, subject.new(string, @args.merge(:separator => separator))
|
270
282
|
|
271
283
|
# it won't change the separator in the strings
|
272
284
|
string = "#{Factory.string}#{separator}#{Factory.string}"
|
273
|
-
|
285
|
+
exp = string
|
286
|
+
assert_equal string, subject.new(string, @args.merge(:separator => separator))
|
274
287
|
|
275
288
|
# it will change the default separator now
|
276
289
|
string = "#{Factory.string}-#{Factory.string}"
|
277
|
-
|
278
|
-
assert_equal
|
290
|
+
exp = string.gsub('-', separator)
|
291
|
+
assert_equal exp, subject.new(string, @args.merge(:separator => separator))
|
279
292
|
end
|
280
293
|
|
281
294
|
should "change underscores into its separator unless allowed" do
|
282
295
|
string = "#{Factory.string}_#{Factory.string}"
|
283
|
-
assert_equal string.gsub('_', '-'), subject.new(string)
|
296
|
+
assert_equal string.gsub('_', '-'), subject.new(string, @args)
|
284
297
|
|
285
|
-
|
286
|
-
assert_equal
|
298
|
+
exp = string.gsub('_', '-')
|
299
|
+
assert_equal exp, subject.new(string, @args.merge(:allow_underscores => false))
|
287
300
|
|
288
|
-
assert_equal string, subject.new(string, :allow_underscores => true)
|
301
|
+
assert_equal string, subject.new(string, @args.merge(:allow_underscores => true))
|
289
302
|
end
|
290
303
|
|
291
304
|
should "not allow multiple separators in a row" do
|
292
305
|
string = "#{Factory.string}--#{Factory.string}"
|
293
|
-
assert_equal string.gsub(/-{2,}/, '-'), subject.new(string)
|
306
|
+
assert_equal string.gsub(/-{2,}/, '-'), subject.new(string, @args)
|
294
307
|
|
295
308
|
# remove separators that were added from changing invalid chars
|
296
309
|
invalid_chars = (Factory.integer(3) + 1).times.map{ NON_WORD_CHARS.choice }.join
|
297
310
|
string = "#{Factory.string}#{invalid_chars}#{Factory.string}"
|
298
|
-
assert_equal string.gsub(/[^\w]+/, '-'), subject.new(string)
|
311
|
+
assert_equal string.gsub(/[^\w]+/, '-'), subject.new(string, @args)
|
299
312
|
end
|
300
313
|
|
301
314
|
should "remove leading and trailing separators" do
|
302
315
|
string = "-#{Factory.string}-#{Factory.string}-"
|
303
|
-
assert_equal string[1..-2], subject.new(string)
|
316
|
+
assert_equal string[1..-2], subject.new(string, @args)
|
304
317
|
|
305
318
|
# remove separators that were added from changing invalid chars
|
306
319
|
invalid_char = NON_WORD_CHARS.choice
|
307
320
|
string = "#{invalid_char}#{Factory.string}-#{Factory.string}#{invalid_char}"
|
308
|
-
assert_equal string[1..-2], subject.new(string)
|
321
|
+
assert_equal string[1..-2], subject.new(string, @args)
|
309
322
|
end
|
310
323
|
|
311
324
|
end
|
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ardb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 27
|
9
|
-
- 0
|
10
|
-
version: 0.27.0
|
4
|
+
version: 0.27.1
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- Kelly Redding
|
@@ -16,98 +10,67 @@ autorequire:
|
|
16
10
|
bindir: bin
|
17
11
|
cert_chain: []
|
18
12
|
|
19
|
-
date:
|
13
|
+
date: 2016-03-09 00:00:00 Z
|
20
14
|
dependencies:
|
21
15
|
- !ruby/object:Gem::Dependency
|
16
|
+
name: assert
|
17
|
+
prerelease: false
|
22
18
|
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
24
19
|
requirements:
|
25
20
|
- - ~>
|
26
21
|
- !ruby/object:Gem::Version
|
27
|
-
hash: 29
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 15
|
31
22
|
version: "2.15"
|
32
23
|
type: :development
|
33
|
-
name: assert
|
34
24
|
version_requirements: *id001
|
35
|
-
prerelease: false
|
36
25
|
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
prerelease: false
|
37
28
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
29
|
requirements:
|
40
30
|
- - ~>
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
hash: 3
|
43
|
-
segments:
|
44
|
-
- 3
|
45
|
-
- 2
|
31
|
+
- &id003 !ruby/object:Gem::Version
|
46
32
|
version: "3.2"
|
47
33
|
type: :runtime
|
48
|
-
name: activerecord
|
49
34
|
version_requirements: *id002
|
50
|
-
prerelease: false
|
51
35
|
- !ruby/object:Gem::Dependency
|
52
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
|
-
requirements:
|
55
|
-
- - ~>
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
hash: 3
|
58
|
-
segments:
|
59
|
-
- 3
|
60
|
-
- 2
|
61
|
-
version: "3.2"
|
62
|
-
type: :runtime
|
63
36
|
name: activesupport
|
64
|
-
version_requirements: *id003
|
65
37
|
prerelease: false
|
66
|
-
- !ruby/object:Gem::Dependency
|
67
38
|
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
-
none: false
|
69
39
|
requirements:
|
70
40
|
- - ~>
|
71
|
-
-
|
72
|
-
hash: 9
|
73
|
-
segments:
|
74
|
-
- 0
|
75
|
-
- 1
|
76
|
-
version: "0.1"
|
41
|
+
- *id003
|
77
42
|
type: :runtime
|
78
|
-
name: much-plugin
|
79
43
|
version_requirements: *id004
|
80
|
-
prerelease: false
|
81
44
|
- !ruby/object:Gem::Dependency
|
45
|
+
name: much-plugin
|
46
|
+
prerelease: false
|
82
47
|
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
48
|
requirements:
|
85
49
|
- - ~>
|
86
50
|
- !ruby/object:Gem::Version
|
87
|
-
|
88
|
-
segments:
|
89
|
-
- 1
|
90
|
-
- 1
|
91
|
-
version: "1.1"
|
51
|
+
version: "0.1"
|
92
52
|
type: :runtime
|
93
|
-
name: ns-options
|
94
53
|
version_requirements: *id005
|
95
|
-
prerelease: false
|
96
54
|
- !ruby/object:Gem::Dependency
|
55
|
+
name: ns-options
|
56
|
+
prerelease: false
|
97
57
|
requirement: &id006 !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
58
|
requirements:
|
100
59
|
- - ~>
|
101
60
|
- !ruby/object:Gem::Version
|
102
|
-
|
103
|
-
segments:
|
104
|
-
- 3
|
105
|
-
- 0
|
106
|
-
version: "3.0"
|
61
|
+
version: "1.1"
|
107
62
|
type: :runtime
|
108
|
-
name: scmd
|
109
63
|
version_requirements: *id006
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: scmd
|
110
66
|
prerelease: false
|
67
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "3.0"
|
72
|
+
type: :runtime
|
73
|
+
version_requirements: *id007
|
111
74
|
description: Activerecord database tools.
|
112
75
|
email:
|
113
76
|
- kelly@kellyredding.com
|
@@ -140,11 +103,13 @@ files:
|
|
140
103
|
- lib/ardb/migration_helpers.rb
|
141
104
|
- lib/ardb/record_spy.rb
|
142
105
|
- lib/ardb/relation_spy.rb
|
106
|
+
- lib/ardb/require_autoloaded_active_record_files.rb
|
143
107
|
- lib/ardb/root_path.rb
|
144
108
|
- lib/ardb/test_helpers.rb
|
145
109
|
- lib/ardb/use_db_default.rb
|
146
110
|
- lib/ardb/version.rb
|
147
111
|
- log/.gitkeep
|
112
|
+
- script/determine_autoloaded_active_record_files.rb
|
148
113
|
- test/helper.rb
|
149
114
|
- test/support/factory.rb
|
150
115
|
- test/unit/adapter/base_tests.rb
|
@@ -179,35 +144,28 @@ files:
|
|
179
144
|
homepage: http://github.com/redding/ardb
|
180
145
|
licenses:
|
181
146
|
- MIT
|
147
|
+
metadata: {}
|
148
|
+
|
182
149
|
post_install_message:
|
183
150
|
rdoc_options: []
|
184
151
|
|
185
152
|
require_paths:
|
186
153
|
- lib
|
187
154
|
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
-
none: false
|
189
155
|
requirements:
|
190
|
-
-
|
156
|
+
- &id008
|
157
|
+
- ">="
|
191
158
|
- !ruby/object:Gem::Version
|
192
|
-
hash: 3
|
193
|
-
segments:
|
194
|
-
- 0
|
195
159
|
version: "0"
|
196
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
|
-
none: false
|
198
161
|
requirements:
|
199
|
-
-
|
200
|
-
- !ruby/object:Gem::Version
|
201
|
-
hash: 3
|
202
|
-
segments:
|
203
|
-
- 0
|
204
|
-
version: "0"
|
162
|
+
- *id008
|
205
163
|
requirements: []
|
206
164
|
|
207
165
|
rubyforge_project:
|
208
|
-
rubygems_version:
|
166
|
+
rubygems_version: 2.5.1
|
209
167
|
signing_key:
|
210
|
-
specification_version:
|
168
|
+
specification_version: 4
|
211
169
|
summary: Activerecord database tools.
|
212
170
|
test_files:
|
213
171
|
- test/helper.rb
|