thoughtbot-pacecar 1.0.0

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/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008, Matt Jankowski & thoughtbot, inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,171 @@
1
+ == Pacecar
2
+
3
+ Pacecar adds named_scope methods to ActiveRecord classes via database column introspection.
4
+
5
+ == Usage
6
+
7
+ Assuming a database schema...
8
+
9
+ class CreateSchema < ActiveRecord::Migration
10
+ def self.up
11
+ create_table :users, :force => true do |t|
12
+ t.boolean :admin, :default => false, :null => false
13
+ t.datetime :approved_at
14
+ t.datetime :rejected_at
15
+ t.string :first_name
16
+ t.string :last_name
17
+ t.text :description
18
+ t.timestamps
19
+ end
20
+ create_table :posts, :force => true do |t|
21
+ t.string :owner_type
22
+ t.integer :owner_id
23
+ t.string :publication_state
24
+ t.string :post_type
25
+ t.timestamps
26
+ end
27
+ create_table :comments, :force => true do |t|
28
+ t.integer :user_id
29
+ t.text :description
30
+ t.timestamps
31
+ end
32
+ end
33
+ end
34
+
35
+ And some basic model declarations...
36
+
37
+ class User < ActiveRecord::Base
38
+ has_many :posts, :as => :owner
39
+ has_many :comments
40
+ scopes_ranking :comments
41
+ end
42
+
43
+ class Post < ActiveRecord::Base
44
+ PUBLICATION_STATES = %w(Draft Submitted Rejected Accepted)
45
+ TYPES = %w(Free Open Private Anonymous PostModern)
46
+ belongs_to :owner, :polymorphic => true
47
+ scopes_state :publication_state
48
+ scopes_state :post_type, :with => TYPES
49
+ scopes_polymorph :owner
50
+ end
51
+
52
+ class Comment < ActiveRecord::Base
53
+ belongs_to :user
54
+ end
55
+
56
+ = All columns
57
+
58
+ Records where approved_at is not null, or where it is null...
59
+
60
+ User.approved_at_present
61
+ User.approved_at_missing
62
+
63
+ Records where first_name is not null, or where it is null...
64
+
65
+ User.first_name_present
66
+ User.first_name_missing
67
+
68
+ Records ordered by first_name (default to 'asc', can specify to override)...
69
+
70
+ User.by_first_name
71
+ User.by_first_name(:asc)
72
+ User.by_first_name(:desc)
73
+
74
+ Records where an attribute matches a search term (column LIKE "%term%")...
75
+
76
+ User.first_name_matches('John')
77
+
78
+ Records where an attribute starts or ends with a search term...
79
+
80
+ User.first_name_starts_with('A')
81
+ User.first_name_ends_with('a')
82
+
83
+ Records where any non-state text or string column matches term...
84
+
85
+ User.search_for('test')
86
+
87
+ Records where any of a list of columns match the term...
88
+
89
+ User.search_for 'test', :on => [:first_name, :last_name]
90
+
91
+ Records where all of a list of columns match the term...
92
+
93
+ User.search_for 'test', :on => [:first_name, :last_name], :require => :all
94
+
95
+ = Boolean columns
96
+
97
+ Records that are all admins or non-admins...
98
+
99
+ User.admin
100
+ User.not_admin
101
+
102
+ = Datetime columns
103
+
104
+ Records approved before or after certain times...
105
+
106
+ User.approved_at_before(5.days.ago)
107
+ User.approved_at_after(4.weeks.ago)
108
+
109
+ Records with approved_at in the past or future...
110
+
111
+ User.approved_at_in_past
112
+ User.approved_at_in_future
113
+
114
+ Records with approved_at inside or outside of two times...
115
+
116
+ User.approved_at_inside(10.days.ago, 1.day.ago)
117
+ User.approved_at_outside(2.days.ago, 1.day.ago)
118
+
119
+ Records with certain year, month or day...
120
+
121
+ User.approved_at_in_year(2000)
122
+ User.approved_at_in_month(01)
123
+ User.approved_at_in_day(01)
124
+
125
+ Records with a duration (time delta between two columns) of, over or under a certain number of days...
126
+
127
+ User.with_duration_of(14, :approved_at, :rejected_at)
128
+ User.with_duration_over(14, :approved_at, :rejected_at)
129
+ User.with_duration_under(14, :approved_at, :rejected_at)
130
+
131
+ = Polymorphic relationships
132
+
133
+ Records which have an owner_type of User...
134
+
135
+ Post.for_owner_type(User)
136
+
137
+ = Associations
138
+
139
+ Records with the most and least associated records...
140
+
141
+ User.maximum_comments
142
+ User.minimum_comments
143
+
144
+ = State columns
145
+
146
+ Records which are in a particular state, or not in a state...
147
+
148
+ Post.publication_state_draft
149
+ Post.post_type_not_open
150
+
151
+ = Limits
152
+
153
+ First x records...
154
+
155
+ User.limited(10)
156
+
157
+ = Named scopes
158
+
159
+ Because these are all named_scope, you can combine them.
160
+
161
+ To get all users that have a first_name set, who are admins and approved more than 2 weeks ago, ordered by their first name...
162
+
163
+ User.first_name_present.admin.approved_at_before(2.weeks.ago).by_first_name
164
+
165
+ To get the top 10 commenters...
166
+
167
+ User.maximim_comments.limited(10)
168
+
169
+ = License
170
+
171
+ Pacecar is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'pacecar'
data/lib/pacecar.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'pacecar/boolean'
2
+ require 'pacecar/datetime'
3
+ require 'pacecar/duration'
4
+ require 'pacecar/helpers'
5
+ require 'pacecar/limit'
6
+ require 'pacecar/order'
7
+ require 'pacecar/polymorph'
8
+ require 'pacecar/presence'
9
+ require 'pacecar/ranking'
10
+ require 'pacecar/search'
11
+ require 'pacecar/state'
12
+
13
+ module Pacecar
14
+ def self.included(base)
15
+ base.class_eval do
16
+ extend ClassMethods
17
+ class << self
18
+ alias_method_chain :inherited, :define_scopes
19
+ end
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+
25
+ protected
26
+
27
+ def inherited_with_define_scopes(child)
28
+ inherited_without_define_scopes(child)
29
+ child.class_eval do
30
+ include Pacecar::Helpers
31
+ include Pacecar::Boolean
32
+ include Pacecar::Datetime
33
+ include Pacecar::Duration
34
+ include Pacecar::Limit
35
+ include Pacecar::Order
36
+ include Pacecar::Polymorph
37
+ include Pacecar::Presence
38
+ include Pacecar::Ranking
39
+ include Pacecar::Search
40
+ include Pacecar::State
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+ ActiveRecord::Base.send :include, Pacecar
@@ -0,0 +1,23 @@
1
+ module Pacecar
2
+ module Boolean
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_boolean_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_boolean_scopes
15
+ boolean_column_names.each do |name|
16
+ named_scope name.to_sym, :conditions => ["#{quoted_table_name}.#{name} = ?", true]
17
+ named_scope "not_#{name}".to_sym, :conditions => ["#{quoted_table_name}.#{name} = ?", false]
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,64 @@
1
+ module Pacecar
2
+ module Datetime
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_datetime_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_datetime_scopes
15
+ datetime_column_names.each do |name|
16
+ define_before_after_scopes(name)
17
+ define_past_future_scopes(name)
18
+ define_inside_outside_scopes(name)
19
+ define_in_date_scopes(name)
20
+ end
21
+ end
22
+
23
+ def define_before_after_scopes(name)
24
+ named_scope "#{name}_before".to_sym, lambda { |time|
25
+ { :conditions => ["#{quoted_table_name}.#{name} < ?", time] }
26
+ }
27
+ named_scope "#{name}_after".to_sym, lambda { |time|
28
+ { :conditions => ["#{quoted_table_name}.#{name} > ?", time] }
29
+ }
30
+ end
31
+
32
+ def define_past_future_scopes(name)
33
+ named_scope "#{name}_in_past", lambda {
34
+ { :conditions => ["#{quoted_table_name}.#{name} < ?", Time.now] }
35
+ }
36
+ named_scope "#{name}_in_future", lambda {
37
+ { :conditions => ["#{quoted_table_name}.#{name} > ?", Time.now] }
38
+ }
39
+ end
40
+
41
+ def define_inside_outside_scopes(name)
42
+ named_scope "#{name}_inside".to_sym, lambda { |start, stop|
43
+ { :conditions => ["#{quoted_table_name}.#{name} > ? and #{quoted_table_name}.#{name} < ?", start, stop] }
44
+ }
45
+ named_scope "#{name}_outside".to_sym, lambda { |start, stop|
46
+ { :conditions => ["#{quoted_table_name}.#{name} < ? and #{quoted_table_name}.#{name} > ?", start, stop] }
47
+ }
48
+ end
49
+
50
+ def define_in_date_scopes(name)
51
+ named_scope "#{name}_in_year".to_sym, lambda { |year|
52
+ { :conditions => ["year(#{quoted_table_name}.#{name}) = ?", year] }
53
+ }
54
+ named_scope "#{name}_in_month".to_sym, lambda { |month|
55
+ { :conditions => ["month(#{quoted_table_name}.#{name}) = ?", month] }
56
+ }
57
+ named_scope "#{name}_in_day".to_sym, lambda { |day|
58
+ { :conditions => ["day(#{quoted_table_name}.#{name}) = ?", day] }
59
+ }
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ module Pacecar
2
+ module Duration
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_duration_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_duration_scopes
15
+ named_scope :with_duration_of, lambda { |duration, start, stop|
16
+ { :conditions => ["datediff(#{quoted_table_name}.#{start}, #{quoted_table_name}.#{stop}) = ?", duration] }
17
+ }
18
+ named_scope :with_duration_over, lambda { |duration, start, stop|
19
+ { :conditions => ["datediff(#{quoted_table_name}.#{start}, #{quoted_table_name}.#{stop}) > ?", duration] }
20
+ }
21
+ named_scope :with_duration_under, lambda { |duration, start, stop|
22
+ { :conditions => ["datediff(#{quoted_table_name}.#{start}, #{quoted_table_name}.#{stop}) < ?", duration] }
23
+ }
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ module Pacecar
2
+ module Helpers
3
+
4
+ mattr_accessor :options
5
+ self.options = {
6
+ :state_pattern => /_(type|state)$/i,
7
+ :default_limit => 10
8
+ }
9
+
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ def column_names
17
+ columns.collect(&:name)
18
+ end
19
+
20
+ def column_names_for_type(*types)
21
+ columns.select { |column| types.include? column.type }.collect(&:name)
22
+ end
23
+
24
+ def column_names_without_type(*types)
25
+ columns.select { |column| ! types.include? column.type }.collect(&:name)
26
+ end
27
+
28
+ def boolean_column_names
29
+ column_names_for_type :boolean
30
+ end
31
+
32
+ def datetime_column_names
33
+ column_names_for_type :datetime
34
+ end
35
+
36
+ def text_and_string_column_names
37
+ column_names_for_type :text, :string
38
+ end
39
+
40
+ def non_state_text_and_string_columns
41
+ text_and_string_column_names.reject { |name| name =~ Pacecar::Helpers.options[:state_pattern] }
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module Pacecar
2
+ module Limit
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_limit_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_limit_scopes
15
+ named_scope :limited, lambda { |*args|
16
+ { :limit => args.flatten.first || (defined?(per_page) ? per_page : Pacecar::Helpers.options[:default_limit]) }
17
+ }
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Pacecar
2
+ module Order
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_order_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_order_scopes
15
+ column_names.each do |name|
16
+ named_scope "by_#{name}".to_sym, lambda { |*args|
17
+ { :order => "#{quoted_table_name}.#{name} #{args.flatten.first || 'asc'}" }
18
+ }
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Pacecar
2
+ module Polymorph
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def scopes_polymorph(name)
10
+ named_scope "for_#{name}_type".to_sym, lambda { |type|
11
+ { :conditions => ["#{quoted_table_name}.#{name}_type = ?", type.to_s] }
12
+ }
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Pacecar
2
+ module Presence
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(base)
9
+ base.send :define_presence_scopes
10
+ end
11
+
12
+ protected
13
+
14
+ def define_presence_scopes
15
+ column_names_without_type(:boolean).each do |name|
16
+ named_scope "#{name}_present".to_sym, :conditions => "#{quoted_table_name}.#{name} is not null"
17
+ named_scope "#{name}_missing".to_sym, :conditions => "#{quoted_table_name}.#{name} is null"
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Pacecar
2
+ module Ranking
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def scopes_ranking(association)
10
+ define_ranking_scope association, :maximum, :desc
11
+ define_ranking_scope association, :minimum, :asc
12
+ end
13
+
14
+ protected
15
+
16
+ def define_ranking_scope(association, name, direction)
17
+ named_scope "#{name}_#{association}",
18
+ :joins => "inner join #{association} on #{association}.#{reflections[association].primary_key_name} = #{quoted_table_name}.#{primary_key}",
19
+ :select => "#{quoted_table_name}.*, count(#{quoted_table_name}.#{primary_key}) as #{association}_count",
20
+ :group => "#{association}.#{reflections[association].primary_key_name}",
21
+ :order => "#{association}_count #{direction}"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ module Pacecar
2
+ module Search
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ 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
+
15
+ def define_search_scopes
16
+ text_and_string_column_names.each do |name|
17
+ named_scope "#{name}_matches".to_sym, lambda { |query|
18
+ { :conditions => ["#{quoted_table_name}.#{name} like :query", { :query => "%#{query}%" }] }
19
+ }
20
+ named_scope "#{name}_starts_with".to_sym, lambda { |query|
21
+ { :conditions => ["#{quoted_table_name}.#{name} like :query", { :query => "#{query}%" }] }
22
+ }
23
+ named_scope "#{name}_ends_with".to_sym, lambda { |query|
24
+ { :conditions => ["#{quoted_table_name}.#{name} like :query", { :query => "%#{query}" }] }
25
+ }
26
+ end
27
+ end
28
+
29
+ def define_basic_search_scope
30
+ named_scope :search_for, lambda { |*args|
31
+ opts = args.extract_options!
32
+ query = args.flatten.first
33
+ columns = opts[:on] || non_state_text_and_string_columns
34
+ joiner = opts[:require].eql?(:all) ? 'and' : 'or'
35
+ match = columns.collect { |name| "#{quoted_table_name}.#{name} like :query" }.join(" #{joiner} ")
36
+ { :conditions => [match, { :query => "%#{query}%" } ] }
37
+ }
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module Pacecar
2
+ module State
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def scopes_state(*names)
10
+ opts = names.extract_options!
11
+ names.each do |name|
12
+ constant = opts[:with] || const_get(name.to_s.pluralize.upcase)
13
+ constant.each do |state|
14
+ named_scope "#{name}_#{state.downcase}".to_sym, :conditions => ["#{quoted_table_name}.#{name} = ?", state]
15
+ named_scope "#{name}_not_#{state.downcase}".to_sym, :conditions => ["#{quoted_table_name}.#{name} <> ?", state]
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thoughtbot-pacecar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Jankowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generated scopes for ActiveRecord classes.
17
+ email: mjankowski@thoughtbot.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - init.rb
26
+ - lib
27
+ - lib/pacecar
28
+ - lib/pacecar/boolean.rb
29
+ - lib/pacecar/datetime.rb
30
+ - lib/pacecar/duration.rb
31
+ - lib/pacecar/helpers.rb
32
+ - lib/pacecar/limit.rb
33
+ - lib/pacecar/order.rb
34
+ - lib/pacecar/polymorph.rb
35
+ - lib/pacecar/presence.rb
36
+ - lib/pacecar/ranking.rb
37
+ - lib/pacecar/search.rb
38
+ - lib/pacecar/state.rb
39
+ - lib/pacecar.rb
40
+ - MIT-LICENSE
41
+ - README.rdoc
42
+ has_rdoc: true
43
+ homepage: http://github.com/thoughtbot/pacecar
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Pacecar adds named_scope methods to ActiveRecord classes via database column introspection.
68
+ test_files: []
69
+