pacecar 1.5.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +9 -1
- data/Appraisals +9 -17
- data/Gemfile +1 -1
- data/Gemfile.lock +93 -108
- data/MIT-LICENSE +1 -1
- data/README.md +10 -109
- data/Rakefile +9 -5
- data/TESTING.md +40 -0
- data/gemfiles/rails_4_mysql2_driver.gemfile +8 -0
- data/gemfiles/rails_4_pg_driver.gemfile +8 -0
- data/gemfiles/rails_4_sqlite3_driver.gemfile +8 -0
- data/lib/pacecar.rb +0 -10
- data/lib/pacecar/boolean.rb +8 -20
- data/lib/pacecar/helpers.rb +7 -16
- data/lib/pacecar/limit.rb +9 -9
- data/lib/pacecar/order.rb +9 -9
- data/lib/pacecar/polymorph.rb +5 -5
- data/lib/pacecar/presence.rb +8 -9
- data/lib/pacecar/search.rb +8 -32
- data/lib/pacecar/state.rb +13 -9
- data/lib/pacecar/version.rb +1 -1
- data/pacecar.gemspec +18 -16
- data/spec/boolean_spec.rb +6 -18
- data/spec/dummy/app/models/mammal.rb +1 -1
- data/spec/dummy/app/models/post.rb +2 -2
- data/spec/dummy/app/models/user.rb +1 -6
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config/application.rb +4 -26
- data/spec/dummy/config/boot.rb +3 -9
- data/spec/dummy/config/database.yml +1 -7
- data/spec/dummy/config/environment.rb +2 -2
- data/spec/dummy/config/environments/development.rb +15 -12
- data/spec/dummy/config/environments/production.rb +57 -26
- data/spec/dummy/config/environments/test.rb +16 -15
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +9 -3
- data/spec/dummy/config/initializers/secret_token.rb +7 -2
- data/spec/dummy/config/initializers/session_store.rb +1 -6
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +20 -2
- data/spec/dummy/config/routes.rb +22 -24
- data/spec/factories.rb +2 -2
- data/spec/helpers_spec.rb +18 -26
- data/spec/integration/navigation_spec.rb +4 -5
- data/spec/limit_spec.rb +11 -11
- data/spec/order_spec.rb +8 -8
- data/spec/pacecar_spec.rb +3 -3
- data/spec/polymorph_spec.rb +6 -6
- data/spec/presence_spec.rb +9 -9
- data/spec/search_spec.rb +14 -48
- data/spec/spec_helper.rb +11 -17
- data/spec/state_spec.rb +29 -29
- metadata +76 -70
- data/gemfiles/rails-3.0.11-database-mysql.gemfile +0 -8
- data/gemfiles/rails-3.0.11-database-mysql.gemfile.lock +0 -134
- data/gemfiles/rails-3.0.11-database-mysql2.gemfile +0 -8
- data/gemfiles/rails-3.0.11-database-mysql2.gemfile.lock +0 -134
- data/gemfiles/rails-3.0.11-database-pg.gemfile +0 -8
- data/gemfiles/rails-3.0.11-database-pg.gemfile.lock +0 -134
- data/gemfiles/rails-3.0.11-database-sqlite3-ruby.gemfile +0 -8
- data/gemfiles/rails-3.0.11-database-sqlite3-ruby.gemfile.lock +0 -136
- data/gemfiles/rails-3.0.11-database-sqlite3.gemfile +0 -8
- data/gemfiles/rails-3.0.11-database-sqlite3.gemfile.lock +0 -134
- data/gemfiles/rails-3.1.3-database-mysql.gemfile +0 -8
- data/gemfiles/rails-3.1.3-database-mysql.gemfile.lock +0 -144
- data/gemfiles/rails-3.1.3-database-mysql2.gemfile +0 -8
- data/gemfiles/rails-3.1.3-database-mysql2.gemfile.lock +0 -144
- data/gemfiles/rails-3.1.3-database-pg.gemfile +0 -8
- data/gemfiles/rails-3.1.3-database-pg.gemfile.lock +0 -144
- data/gemfiles/rails-3.1.3-database-sqlite3-ruby.gemfile +0 -8
- data/gemfiles/rails-3.1.3-database-sqlite3-ruby.gemfile.lock +0 -146
- data/gemfiles/rails-3.1.3-database-sqlite3.gemfile +0 -8
- data/gemfiles/rails-3.1.3-database-sqlite3.gemfile.lock +0 -144
- data/lib/pacecar/associations.rb +0 -39
- data/lib/pacecar/datetime.rb +0 -91
- data/lib/pacecar/duration.rb +0 -51
- data/lib/pacecar/numeric.rb +0 -29
- data/lib/pacecar/ranking.rb +0 -53
- data/spec/associations_spec.rb +0 -32
- data/spec/datetime_spec.rb +0 -92
- data/spec/duration_spec.rb +0 -22
- data/spec/numeric_spec.rb +0 -43
- data/spec/ranking_spec.rb +0 -63
data/Rakefile
CHANGED
@@ -5,10 +5,14 @@ require 'appraisal'
|
|
5
5
|
require 'rspec/core/rake_task'
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
task :default do |t|
|
9
|
+
if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
|
10
|
+
exec 'rake spec'
|
11
|
+
else
|
12
|
+
Rake::Task['appraise'].execute
|
13
|
+
end
|
14
|
+
end
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
exec('rake appraisal spec')
|
16
|
+
task appraise: ['appraisal:install'] do |t|
|
17
|
+
exec 'rake appraisal'
|
14
18
|
end
|
data/TESTING.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
Running the test suite
|
2
|
+
======================
|
3
|
+
|
4
|
+
|
5
|
+
Run the tests:
|
6
|
+
|
7
|
+
% rake
|
8
|
+
|
9
|
+
In order to do this you must have gems bundled, Appraisal set up, and
|
10
|
+
three databases configured.
|
11
|
+
|
12
|
+
bundler
|
13
|
+
-------
|
14
|
+
|
15
|
+
% bundle --binstubs
|
16
|
+
|
17
|
+
Appraisal
|
18
|
+
---------
|
19
|
+
|
20
|
+
% rake appraisal:install
|
21
|
+
|
22
|
+
MySQL
|
23
|
+
-----
|
24
|
+
|
25
|
+
% echo create database pacecar_test | mysql
|
26
|
+
% cd spec/dummy
|
27
|
+
spec/dummy% RAILS_ENV=test BUNDLE_GEMFILE=../../gemfiles/rails_4_mysql2_driver.gemfile bundle exec rake db:migrate
|
28
|
+
|
29
|
+
Postgres
|
30
|
+
--------
|
31
|
+
|
32
|
+
% createdb pacecar_test
|
33
|
+
% cd spec/dummy
|
34
|
+
spec/dummy% RAILS_ENV=test BUNDLE_GEMFILE=../../gemfiles/rails_4_pg_driver.gemfile bundle exec rake db:migrate
|
35
|
+
|
36
|
+
sqlite
|
37
|
+
------
|
38
|
+
|
39
|
+
% cd spec/dummy
|
40
|
+
spec/dummy% RAILS_ENV=test % BUNDLE_GEMFILE=../../gemfiles/rails_4_sqlite3_driver.gemfile bundle exec rake db:migrate
|
data/lib/pacecar.rb
CHANGED
@@ -1,33 +1,23 @@
|
|
1
|
-
require 'pacecar/associations'
|
2
1
|
require 'pacecar/boolean'
|
3
|
-
require 'pacecar/datetime'
|
4
|
-
require 'pacecar/duration'
|
5
2
|
require 'pacecar/helpers'
|
6
3
|
require 'pacecar/limit'
|
7
4
|
require 'pacecar/order'
|
8
5
|
require 'pacecar/polymorph'
|
9
6
|
require 'pacecar/presence'
|
10
|
-
require 'pacecar/ranking'
|
11
7
|
require 'pacecar/search'
|
12
8
|
require 'pacecar/state'
|
13
|
-
require 'pacecar/numeric'
|
14
9
|
require 'pacecar/version'
|
15
10
|
|
16
11
|
module Pacecar
|
17
12
|
def self.included(base)
|
18
13
|
base.class_eval do
|
19
|
-
include Pacecar::Associations
|
20
14
|
include Pacecar::Boolean
|
21
|
-
include Pacecar::Datetime
|
22
|
-
include Pacecar::Duration
|
23
15
|
include Pacecar::Limit
|
24
16
|
include Pacecar::Order
|
25
17
|
include Pacecar::Polymorph
|
26
18
|
include Pacecar::Presence
|
27
|
-
include Pacecar::Ranking
|
28
19
|
include Pacecar::Search
|
29
20
|
include Pacecar::State
|
30
|
-
include Pacecar::Numeric
|
31
21
|
end
|
32
22
|
end
|
33
23
|
end
|
data/lib/pacecar/boolean.rb
CHANGED
@@ -1,31 +1,19 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Boolean
|
3
|
-
|
4
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_boolean_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
module ClassMethods
|
8
|
-
def self.extended(base)
|
9
|
-
base.send :define_boolean_scopes
|
10
|
-
base.send :define_balance_count
|
11
|
-
end
|
12
|
-
|
13
|
-
protected
|
14
12
|
|
15
13
|
def define_boolean_scopes
|
16
14
|
boolean_column_names.each do |name|
|
17
|
-
scope name
|
18
|
-
scope "not_#{name}"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def define_balance_count
|
23
|
-
boolean_column_names.each do |name|
|
24
|
-
self.class_eval %Q{
|
25
|
-
def self.#{name}_balance
|
26
|
-
#{name}.count - not_#{name}.count
|
27
|
-
end
|
28
|
-
}
|
15
|
+
scope name, -> { where(name => true) }
|
16
|
+
scope "not_#{name}", -> { where(name => false) }
|
29
17
|
end
|
30
18
|
end
|
31
19
|
|
data/lib/pacecar/helpers.rb
CHANGED
@@ -1,16 +1,15 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Helpers
|
5
|
+
extend ActiveSupport::Concern
|
3
6
|
|
4
7
|
mattr_accessor :options
|
5
8
|
self.options = {
|
6
|
-
:
|
7
|
-
:
|
9
|
+
state_pattern: /_(type|state)$/i,
|
10
|
+
default_limit: 10
|
8
11
|
}
|
9
12
|
|
10
|
-
def self.included(base)
|
11
|
-
base.extend ClassMethods
|
12
|
-
end
|
13
|
-
|
14
13
|
module ClassMethods
|
15
14
|
|
16
15
|
def safe_column_names
|
@@ -25,10 +24,6 @@ module Pacecar
|
|
25
24
|
column_names_for_type :boolean
|
26
25
|
end
|
27
26
|
|
28
|
-
def datetime_column_names
|
29
|
-
column_names_for_type :datetime, :date
|
30
|
-
end
|
31
|
-
|
32
27
|
def text_and_string_column_names
|
33
28
|
column_names_for_type :text, :string
|
34
29
|
end
|
@@ -37,10 +32,6 @@ module Pacecar
|
|
37
32
|
text_and_string_column_names.reject { |name| name =~ Pacecar::Helpers.options[:state_pattern] }
|
38
33
|
end
|
39
34
|
|
40
|
-
def numeric_column_names
|
41
|
-
column_names_for_type :integer, :float, :decimal
|
42
|
-
end
|
43
|
-
|
44
35
|
protected
|
45
36
|
|
46
37
|
def safe_columns
|
@@ -71,8 +62,8 @@ module Pacecar
|
|
71
62
|
end
|
72
63
|
|
73
64
|
def column_names_without_type(*types)
|
74
|
-
safe_columns.
|
75
|
-
end
|
65
|
+
safe_columns.reject { |column| types.include? column.type }.collect(&:name)
|
66
|
+
end
|
76
67
|
|
77
68
|
end
|
78
69
|
end
|
data/lib/pacecar/limit.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Limit
|
3
|
-
|
4
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_limit_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
module ClassMethods
|
8
|
-
def self.extended(base)
|
9
|
-
base.send :define_limit_scopes
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
12
|
|
14
13
|
def define_limit_scopes
|
15
|
-
scope :limited,
|
16
|
-
|
14
|
+
scope :limited, ->(*args) {
|
15
|
+
value = args.flatten.first || (defined?(per_page) ? per_page : Pacecar::Helpers.options[:default_limit])
|
16
|
+
limit(value)
|
17
17
|
}
|
18
18
|
end
|
19
19
|
|
data/lib/pacecar/order.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Order
|
3
|
-
|
4
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_order_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
module ClassMethods
|
8
|
-
def self.extended(base)
|
9
|
-
base.send :define_order_scopes
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
12
|
|
14
13
|
def define_order_scopes
|
15
14
|
safe_column_names.each do |name|
|
16
|
-
scope "by_#{name}"
|
17
|
-
|
15
|
+
scope "by_#{name}", ->(*args) {
|
16
|
+
direction = args.flatten.first || 'asc'
|
17
|
+
order("#{name} #{direction}")
|
18
18
|
}
|
19
19
|
end
|
20
20
|
end
|
data/lib/pacecar/polymorph.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Polymorph
|
3
|
-
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
5
|
+
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
|
9
9
|
def has_polymorph(name)
|
10
|
-
scope "for_#{name}_type"
|
10
|
+
scope "for_#{name}_type", ->(type) {
|
11
11
|
polymorph_type = "#{name}_type"
|
12
|
-
|
12
|
+
where(polymorph_type => type)
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
data/lib/pacecar/presence.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Presence
|
3
|
-
|
4
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_presence_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
module ClassMethods
|
8
|
-
def self.extended(base)
|
9
|
-
base.send :define_presence_scopes
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
12
|
|
14
13
|
def define_presence_scopes
|
15
14
|
non_boolean_column_names.each do |name|
|
16
|
-
scope "#{name}_present"
|
17
|
-
scope "#{name}_missing"
|
15
|
+
scope "#{name}_present", -> { where(arel_table[name].not_eq nil) }
|
16
|
+
scope "#{name}_missing", -> { where(arel_table[name].eq nil) }
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
data/lib/pacecar/search.rb
CHANGED
@@ -1,47 +1,23 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module Search
|
3
|
-
|
4
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_search_scopes
|
5
9
|
end
|
6
10
|
|
7
11
|
module ClassMethods
|
8
|
-
def self.extended(base)
|
9
|
-
base.send :define_search_scopes
|
10
|
-
base.send :define_basic_search_scope
|
11
|
-
end
|
12
|
-
|
13
|
-
protected
|
14
12
|
|
15
13
|
def define_search_scopes
|
16
14
|
safe_column_names.each do |name|
|
17
|
-
scope "#{name}_equals"
|
18
|
-
|
19
|
-
}
|
20
|
-
end
|
21
|
-
text_and_string_column_names.each do |name|
|
22
|
-
scope "#{name}_matches".to_sym, lambda { |query|
|
23
|
-
{ :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "%#{query}%" }] }
|
24
|
-
}
|
25
|
-
scope "#{name}_starts_with".to_sym, lambda { |query|
|
26
|
-
{ :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "#{query}%" }] }
|
27
|
-
}
|
28
|
-
scope "#{name}_ends_with".to_sym, lambda { |query|
|
29
|
-
{ :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "%#{query}" }] }
|
15
|
+
scope "#{name}_equals", ->(query) {
|
16
|
+
where(name => query)
|
30
17
|
}
|
31
18
|
end
|
32
19
|
end
|
33
20
|
|
34
|
-
def define_basic_search_scope
|
35
|
-
scope :search_for, lambda { |*args|
|
36
|
-
opts = args.extract_options!
|
37
|
-
query = args.flatten.first
|
38
|
-
columns = opts[:on] || non_state_text_and_string_columns
|
39
|
-
joiner = opts[:require].eql?(:all) ? 'AND' : 'OR'
|
40
|
-
match = columns.collect { |name| "lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)" }.join(" #{joiner} ")
|
41
|
-
{ :conditions => [match, { :query => "%#{query}%" } ] }
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
21
|
end
|
46
22
|
end
|
47
23
|
end
|
data/lib/pacecar/state.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
module Pacecar
|
2
4
|
module State
|
3
|
-
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
5
|
+
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
|
@@ -11,8 +11,12 @@ module Pacecar
|
|
11
11
|
names.each do |name|
|
12
12
|
constant = opts[:with] || const_get(name.to_s.pluralize.upcase)
|
13
13
|
constant.each do |state|
|
14
|
-
scope "#{name}_#{state.downcase}"
|
15
|
-
|
14
|
+
scope "#{name}_#{state.downcase}", -> {
|
15
|
+
where(arel_table[name].eq state)
|
16
|
+
}
|
17
|
+
scope "#{name}_not_#{state.downcase}", -> {
|
18
|
+
where(arel_table[name].not_eq state)
|
19
|
+
}
|
16
20
|
self.class_eval %Q{
|
17
21
|
def #{name}_#{state.downcase}?
|
18
22
|
#{name} == '#{state}'
|
@@ -22,11 +26,11 @@ module Pacecar
|
|
22
26
|
end
|
23
27
|
}
|
24
28
|
end
|
25
|
-
scope "#{name}"
|
26
|
-
|
29
|
+
scope "#{name}", ->(state) {
|
30
|
+
where(arel_table[name].eq state)
|
27
31
|
}
|
28
|
-
scope "#{name}_not"
|
29
|
-
|
32
|
+
scope "#{name}_not", ->(state) {
|
33
|
+
where(arel_table[name].not_eq state)
|
30
34
|
}
|
31
35
|
end
|
32
36
|
end
|
data/lib/pacecar/version.rb
CHANGED
data/pacecar.gemspec
CHANGED
@@ -1,25 +1,27 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'pacecar/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name
|
7
|
-
s.version
|
8
|
-
s.platform
|
9
|
-
s.authors
|
10
|
-
s.email
|
11
|
-
s.homepage
|
12
|
-
s.summary
|
13
|
-
s.description
|
6
|
+
s.name = 'pacecar'
|
7
|
+
s.version = Pacecar::VERSION.dup
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Matt Jankowski', 'Gabe Berke-Williams', 'Chad Pytel', 'Ryan McGeary', 'Mike Burns', 'Ryan Sonnek', 'Thomas Dippel', 'Tristan Dunn']
|
10
|
+
s.email = 'support@thoughtbot.com'
|
11
|
+
s.homepage = 'http://github.com/thoughtbot/pacecar'
|
12
|
+
s.summary = 'Pacecar adds scope methods to ActiveRecord classes via database column introspection.'
|
13
|
+
s.description = 'Generated scopes for ActiveRecord classes.'
|
14
14
|
|
15
15
|
s.files = `git ls-files`.split("\n")
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
-
s.require_paths = [
|
18
|
+
s.require_paths = ['lib']
|
19
19
|
|
20
|
-
s.
|
21
|
-
|
22
|
-
s.add_development_dependency(
|
23
|
-
s.add_development_dependency(
|
24
|
-
s.add_development_dependency(
|
20
|
+
s.add_dependency('activerecord', '>= 4.0.0')
|
21
|
+
|
22
|
+
s.add_development_dependency('appraisal')
|
23
|
+
s.add_development_dependency('mocha')
|
24
|
+
s.add_development_dependency('rspec-rails')
|
25
|
+
s.add_development_dependency('factory_girl_rails')
|
26
|
+
s.add_development_dependency('rails')
|
25
27
|
end
|