by_star 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -335,11 +335,27 @@ All methods support this extra option.
335
335
 
336
336
  ## Scoping the find
337
337
 
338
- All the `by_*` methods takes a block which will then scope the find based on the options passed into it. The supported options are the same options that are supported by `ActiveRecord::Base.find`:
338
+ All the `by_*` methods take a block which will then scope the find based on the options passed into it. You can also specify these options for each method, but the syntax may differ. The supported options are the same options that are supported by `find` from ActiveRecord. Please note that if you want to use conditions you *have* to use this syntax:
339
+
340
+ Post.by_month(1) { { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] } }
341
+
342
+ or the lengthened:
339
343
 
340
344
  Post.by_month(1) do
341
345
  { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] }
342
346
  end
347
+
348
+ An alternative syntax to this is:
349
+
350
+ Post.by_month(1, { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] })
351
+
352
+ ## Ordering records
353
+
354
+ To order the returned set of records you may specify an `:order` option which works the same was as a standard AR `:order` option:
355
+
356
+ Item.by_month(1, :order => "position DESC")
357
+
358
+
343
359
  ## "Chronicable string"
344
360
 
345
361
  This means a string that can be parsed with the Chronic gem.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
data/by_star.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{by_star}
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Bigg", "Mislav Marohni\304\207"]
12
- s.date = %q{2009-10-28}
12
+ s.date = %q{2009-10-30}
13
13
  s.description = %q{ActiveRecord extension for easier date scopes and time ranges}
14
14
  s.email = %q{radarlistener@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,15 +1,20 @@
1
1
  module ByStar
2
2
  module Calculations
3
3
  module Count
4
- def count_by_year(field=nil, year=Time.now.year, options={})
5
- count(field, :conditions => conditions_for_range(start_of_year(year), end_of_year(year), options))
4
+ def count_by_year(field=nil, year=Time.now.year, options={}, &block)
5
+ db_field = options.delete(:field)
6
+ scoped_by(block) do
7
+ count(field, { :conditions => conditions_for_range(start_of_year(year), end_of_year(year), db_field) }.reverse_merge!(options))
8
+ end
6
9
  end
7
10
 
8
- def count_by_month(field=nil, month=Time.now.month, options={})
9
- year, month = work_out_month(month, options)
10
- count(field, :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), options))
11
+ def count_by_month(field=nil, month=Time.now.month, options={}, &block)
12
+ db_field = options.delete(:field)
13
+ year, month = work_out_month(month, options.delete(:year))
14
+ scoped_by(block) do
15
+ count(field, { :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), db_field) }.reverse_merge!(options))
16
+ end
11
17
  end
12
18
  end
13
-
14
19
  end
15
20
  end
@@ -1,13 +1,17 @@
1
1
  module ByStar
2
2
  module Calculations
3
3
  module Sum
4
- def sum_by_year(field, year=Time.zone.now.year, options={})
5
- sum(field, :conditions => conditions_for_range(start_of_year(year), end_of_year(year), options))
4
+ def sum_by_year(field, year=Time.zone.now.year, options={}, &block)
5
+ scoped_by(block) do
6
+ sum(field, { :conditions => conditions_for_range(start_of_year(year), end_of_year(year), options.delete(:field)) }.reverse_merge!(options))
7
+ end
6
8
  end
7
9
 
8
- def sum_by_month(field, month=Time.zone.now.month, options={})
9
- year, month = work_out_month(month, options)
10
- sum(field, :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), options))
10
+ def sum_by_month(field, month=Time.zone.now.month, options={}, &block)
11
+ year, month = work_out_month(month, options.delete(:year))
12
+ scoped_by(block) do
13
+ sum(field, { :conditions => conditions_for_range(start_of_month(month, year), end_of_month(month, year), options.delete(:field)) }.reverse_merge!(options))
14
+ end
11
15
  end
12
16
  end
13
17
  end
data/lib/calculations.rb CHANGED
@@ -4,8 +4,8 @@ module ByStar
4
4
  include Sum
5
5
 
6
6
  private
7
- def work_out_month(time, options = {})
8
- year = options[:year] ||= Time.zone.now.year
7
+ def work_out_month(time, year=Time.zone.now.year)
8
+ year ||= Time.zone.now.year
9
9
  # Work out what actual month is.
10
10
  month = if time.is_a?(Numeric) && (1..12).include?(time)
11
11
  time
@@ -3,15 +3,15 @@ module ByStar
3
3
 
4
4
  private
5
5
 
6
- def start_of_year(year=Time.now.year)
6
+ def start_of_year(year=Time.zone.now.year)
7
7
  Time.utc(year, 1, 1)
8
8
  end
9
9
 
10
- def end_of_year(year=Time.now.year)
10
+ def end_of_year(year=Time.zone.now.year)
11
11
  start_of_year.end_of_year
12
12
  end
13
13
 
14
- def start_of_month(month, year=Time.now.year)
14
+ def start_of_month(month, year=Timeow.year)
15
15
  Time.utc(year, month, 1)
16
16
  end
17
17
 
data/lib/shared.rb CHANGED
@@ -1,6 +1,17 @@
1
1
  module Shared
2
- def conditions_for_range(start_time, end_time, options = {})
3
- field = connection.quote_table_name(table_name) << '.' << connection.quote_column_name(options[:field] || "created_at")
2
+ def conditions_for_range(start_time, end_time, field="created_at")
3
+ field = connection.quote_table_name(table_name) << '.' << connection.quote_column_name(field || "created_at")
4
4
  ["#{field} >= ? AND #{field} <= ?", start_time.utc, end_time.utc]
5
5
  end
6
+
7
+ private
8
+ def scoped_by(options=nil, &block)
9
+ if options && scope = options.call
10
+ with_scope(:find => scope) do
11
+ block.call
12
+ end
13
+ else
14
+ block.call
15
+ end
16
+ end
6
17
  end
data/lib/vanilla.rb CHANGED
@@ -24,7 +24,7 @@ module ByStar
24
24
  # by_month(time)
25
25
  def by_month(time=Time.zone.now.month, options={}, &block)
26
26
  time = Time.zone.now.month if time.nil?
27
- year, month = work_out_month(time, options)
27
+ year, month = work_out_month(time, options.delete(:year))
28
28
 
29
29
  start_time = start_of_month(month, year)
30
30
  end_time = start_time.end_of_month
@@ -71,7 +71,7 @@ module ByStar
71
71
  time = parse(time)
72
72
 
73
73
  # If options[:year] is passed in, use that year regardless.
74
- year = work_out_year(options[:year]) if options[:year]
74
+ year = work_out_year(options.delete(:year)) if options[:year]
75
75
  # If the first argument is a date or time, ask it for the year
76
76
  year ||= time.year unless time.is_a?(Numeric)
77
77
  # If the first argument is a fixnum, assume this year.
@@ -180,13 +180,11 @@ module ByStar
180
180
 
181
181
  def by_direction(condition, time, options = {}, &block)
182
182
  field = connection.quote_table_name(table_name)
183
- field << "." << connection.quote_column_name(options[:field] || "created_at")
184
- with_scope(:find => { :conditions => ["#{field} #{condition} ?", time.utc] }) do
185
- if block_given?
186
- with_scope(:find => block.call) do
187
- find(:all)
188
- end
189
- else
183
+ field << "." << connection.quote_column_name(options.delete(:field) || "created_at")
184
+ validate_find_options(options)
185
+ scoping = { :conditions => ["#{field} #{condition} ?", time.utc] }.merge(options)
186
+ with_scope(:find => scoping) do
187
+ scoped_by(block) do
190
188
  find(:all)
191
189
  end
192
190
  end
@@ -198,15 +196,11 @@ module ByStar
198
196
  end_time = parse(end_time)
199
197
 
200
198
  raise ParseError, "End time is before start time, searching like this will return no results." if end_time < start_time
201
- order = options.delete(:order)
202
- scoping = { :conditions => conditions_for_range(start_time, end_time, options) }
203
- scoping.merge!(:order => order) if order
199
+ field = options.delete(:field)
200
+ validate_find_options(options)
201
+ scoping = { :conditions => conditions_for_range(start_time, end_time, field) }.merge(options)
204
202
  with_scope(:find => scoping) do
205
- if block_given?
206
- with_scope(:find => block.call) do
207
- find(:all)
208
- end
209
- else
203
+ scoped_by(block) do
210
204
  find(:all)
211
205
  end
212
206
  end
data/spec/by_star_spec.rb CHANGED
@@ -310,6 +310,11 @@ describe Post do
310
310
  it "should be able to find all events before Ryan's birthday using a non-standard field" do
311
311
  Event.past("04-12-#{Time.zone.now.year}".to_time, :field => "start_time").size.should eql(7)
312
312
  end
313
+
314
+ it "should be able to order the find" do
315
+ find(Date.today, :order => "created_at ASC").first.text.should eql("Last year")
316
+ find(Date.today, :order => "created_at DESC").first.text.should eql("post 0")
317
+ end
313
318
 
314
319
  end
315
320
 
@@ -492,6 +497,15 @@ describe Post do
492
497
  { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
493
498
  end.size.should eql(1)
494
499
  end
500
+
501
+ it "should work when block is empty" do
502
+ stub_time
503
+ Post.future { }.size.should eql(71)
504
+ end
505
+
506
+ it "should be able to find a single post from the future with the tag 'tomorrow' (redux)" do
507
+ Post.future(Time.zone.now, :include => :tags, :conditions => ["tags.name = ?", 'tomorrow']).size.should eql(1)
508
+ end
495
509
 
496
510
  end
497
511
 
@@ -525,6 +539,12 @@ describe Post do
525
539
  it "different year" do
526
540
  Invoice.count_by_year(:all, 2008)
527
541
  end
542
+
543
+ it "current year with the given tag" do
544
+ Post.count_by_year do
545
+ { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
546
+ end.should eql(1)
547
+ end
528
548
  end
529
549
 
530
550
  describe "by month" do
@@ -538,7 +558,17 @@ describe Post do
538
558
 
539
559
  it "different month" do
540
560
  stub_time
541
- Invoice.count_by_month(:all, 9).should eql(9)
561
+ Invoice.count_by_month(:all, 9)
562
+ end
563
+
564
+ it "current month with the given tag" do
565
+ Post.count_by_month(:all, Time.zone.now) do
566
+ { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
567
+ end.should eql(1)
568
+ end
569
+
570
+ it "current month with blank block" do
571
+ Post.count_by_month(:all, Time.zone.now) { }.should eql(16)
542
572
  end
543
573
  end
544
574
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: by_star
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-28 00:00:00 +10:00
13
+ date: 2009-10-30 00:00:00 +10:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency