Fingertips-as_new-san 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/as_new-san.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{as_new-san}
5
- s.version = "0.1.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Eloy Duran"]
9
- s.date = %q{2009-03-09}
9
+ s.date = %q{2009-07-15}
10
10
  s.description = %q{The AsNewSan mixin makes it easier to create associations on a new Active Record instance. Use the as_new method to instantiate new empty objects that are immediately saved to the database with a special flag marking them as new. Because new instances are already stored in the database, you always have an id available for creating associations. This means you can use the same views and controller logic for new and edit actions which is especially helpful when you are creating new associated objects using Ajax calls.}
11
11
  s.email = %q{eloy.de.enige@gmail.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "LICENSE"]
data/lib/as_new_san.rb CHANGED
@@ -49,10 +49,22 @@
49
49
  # # Remove all records older than 1 week that have the `as_new` property set to `true`.
50
50
  # Message.collect_garbage!
51
51
  #
52
- # # The mixin comes with the `find_without_as_new` class method, which behaves as the
53
- # # ActiveRecord::Base#find class method, but only finds records which have their
54
- # # `as_new` property set to `false`.
55
- # @messages = Message.find_without_as_new(:all)
52
+ # # The mixin adds an `exclude_as_new` named_scope and makes `#find` and `#count` use it
53
+ # # out of the box.
54
+ # @messages = Message.find(:all) # finds only records that have `as_new` set to `false`
55
+ # @messages = Message.count(:all) # counts only records that have `as_new` set to `false`
56
+ #
57
+ # # If you need to find or count all records, even the ones with `as_new` set to `true`,
58
+ # # the mixin also comes with the `find_including_new_records` and `count_including_new_records`
59
+ # # class methods, which behave the same as ActiveRecord::Base#find and ActiveRecord::Base.count.
60
+ # @messages = Message.find_including_new_records(:all) # finds all records
61
+ # @messages = Message.count_including_new_records(:all) # counts all records
62
+ #
63
+ # # If you need to reload a record that has beed created with `as_new`, `reload` won't work.
64
+ # # You need to use `#find_including_new_records`.
65
+ # @message = Message.as_new
66
+ # @message.reload # => ActiveRecord::RecordNotFound
67
+ # @message = Message.find_including_new_records(@message.id)
56
68
  # end
57
69
  #
58
70
  # def new
@@ -65,7 +77,7 @@
65
77
  # def update
66
78
  # # The mixin comes with a `before_update` filter that sets the `as_new` column to
67
79
  # # `false` before saving the updated record.
68
- # @message = Message.find(params[:id])
80
+ # @message = Message.find_including_new_records(params[:id])
69
81
  # @message.update_attributes(params[:message])
70
82
  #
71
83
  # # So, at this point the record is not marked `as_new` anymore.
@@ -73,8 +85,9 @@
73
85
  # end
74
86
  module AsNewSan
75
87
  def self.included(base) #:nodoc:
76
- base.extend(ClassMethods)
77
88
  base.before_update(:unset_as_new)
89
+ base.named_scope :exclude_as_new, { :conditions => { :as_new => false } }
90
+ base.extend(ClassMethods)
78
91
  end
79
92
 
80
93
  module ClassMethods
@@ -94,14 +107,28 @@ module AsNewSan
94
107
  # Finds and destroys all the records where the as_new column is set to
95
108
  # +true+ and +created_at+ is longer than 1 week ago.
96
109
  def collect_garbage!
97
- find(:all, :conditions => ['as_new = ? AND created_at < ?', true, Time.now - 1.week]).each(&:destroy)
110
+ find_including_new_records(:all, :conditions => ['as_new = ? AND created_at < ?', true, Time.now - 1.week]).each(&:destroy)
98
111
  end
99
112
 
100
113
  # Works the same as <tt>ActiveRecord::Base.find</tt> but only returns
101
114
  # records for which the as_new column is set to +false+.
102
- def find_without_as_new(*args)
103
- with_scope(:find => { :conditions => { :as_new => false } }) do
104
- find(*args)
115
+ def find_excluding_new_records(*args)
116
+ exclude_as_new.find_including_new_records(*args)
117
+ end
118
+
119
+ # Works the same as <tt>ActiveRecord::Base.count</tt> but only returns
120
+ # records for which the as_new column is set to +false+.
121
+ def count_excluding_new_records(*args)
122
+ exclude_as_new.count_including_new_records(*args)
123
+ end
124
+
125
+ def self.extended(klass)
126
+ class << klass
127
+ alias_method :find_including_new_records, :find
128
+ alias_method :find, :find_excluding_new_records
129
+
130
+ alias_method :count_including_new_records, :count
131
+ alias_method :count, :count_excluding_new_records
105
132
  end
106
133
  end
107
134
  end
@@ -117,4 +144,4 @@ module AsNewSan
117
144
  self.as_new = false
118
145
  true
119
146
  end
120
- end
147
+ end
@@ -1,32 +1,37 @@
1
1
  require File.expand_path('../test_helper', __FILE__)
2
2
 
3
- describe "AsNewSan" do
3
+ describe "AsNewSan", ActiveRecord::TestCase do
4
4
  include DBSetupAndTeardownHelper
5
5
 
6
- it "should really create a record for a `new` object with `as_new` set to `true`" do
7
- lambda { BaconFlavour.as_new(:name => 'chunky') }.should.differ('BaconFlavour.count', +1)
8
- BaconFlavour.find_by_name('chunky').as_new.should.be true
6
+ it "should really create a record for a `new' object with `as_new' set to `true'" do
7
+ assert_difference('BaconFlavour.count_including_new_records', +1) { BaconFlavour.as_new(:name => 'chunky') }
8
+ BaconFlavour.find_including_new_records(:first, :conditions => { :name => 'chunky' }).as_new.should.be true
9
9
  end
10
10
 
11
11
  it "should garbage collect any record which has been in the db for a specific period" do
12
12
  old_record = BaconFlavour.as_new(:name => 'banana', :created_at => (Time.now - 1.week - 1))
13
13
  new_record = BaconFlavour.as_new(:name => 'smoked')
14
14
 
15
- lambda { BaconFlavour.collect_garbage! }.should.differ('BaconFlavour.count', -1)
15
+ assert_difference('BaconFlavour.count_including_new_records', -1) { BaconFlavour.collect_garbage! }
16
16
  end
17
17
 
18
- it "should be possible to do a `find` without matching any `as_new` records" do
18
+ it "should be possible to do a `find' including `as_new' records" do
19
19
  as_new_record = BaconFlavour.as_new(:name => 'smells as new')
20
20
  not_as_new_record = BaconFlavour.create(:name => 'does not smell at all')
21
21
 
22
- BaconFlavour.find(:all).should == [as_new_record, not_as_new_record]
23
- BaconFlavour.find_without_as_new(:all).should == [not_as_new_record]
22
+ BaconFlavour.find_including_new_records(:all).should == [as_new_record, not_as_new_record]
23
+ BaconFlavour.find(:all).should == [not_as_new_record]
24
24
  end
25
25
 
26
- it "should set `as_new` records to `false` on update" do
26
+ it "should set `as_new' records to `false' on update" do
27
27
  record = BaconFlavour.as_new(:name => 'ice cream')
28
28
  record.as_new_record?.should.be true
29
29
  record.update_attribute(:name, 'that is right, bacon ice cream')
30
30
  record.as_new_record?.should.be false
31
31
  end
32
+
33
+ it "should define a named scope to exclude as_new records" do
34
+ as_new_record = BaconFlavour.as_new(:name => 'good as new')
35
+ BaconFlavour.exclude_as_new.find_including_new_records(:first, :conditions => { :name => 'good as new' }).should.be.nil
36
+ end
32
37
  end
data/test/test_helper.rb CHANGED
@@ -1,50 +1,31 @@
1
1
  require "rubygems"
2
- require "test/unit"
3
2
  require "test/spec"
4
3
  require 'activerecord'
5
4
 
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
-
5
+ $:.unshift File.expand_path('../../lib', __FILE__)
9
6
  require File.expand_path('../../rails/init', __FILE__)
10
7
 
11
8
  module DBSetupAndTeardownHelper
12
9
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
13
10
  ActiveRecord::Migration.verbose = false
14
11
 
15
- def self.included(base)
16
- base.class_eval do
17
- before do
18
- ActiveRecord::Schema.define(:version => 1) do
19
- create_table :bacon_flavours do |t|
20
- t.string :name
21
- t.boolean :as_new, :default => false
22
- t.timestamps
23
- end
24
- end
25
- end
26
-
27
- after do
28
- ActiveRecord::Base.connection.tables.each do |table|
29
- ActiveRecord::Base.connection.drop_table(table)
30
- end
12
+ def setup
13
+ ActiveRecord::Schema.define(:version => 1) do
14
+ create_table :bacon_flavours do |t|
15
+ t.string :name
16
+ t.boolean :as_new, :default => false
17
+ t.timestamps
31
18
  end
32
19
  end
33
20
  end
34
- end
35
-
36
- class BaconFlavour < ActiveRecord::Base
37
- include AsNewSan
38
- end
39
-
40
- module Test::Spec::Rails
41
- module ShouldDiffer
42
- def differ(eval_str, diff)
43
- before = eval(eval_str)
44
- @object.call
45
- assert_equal before + diff, eval(eval_str)
21
+
22
+ def teardown
23
+ ActiveRecord::Base.connection.tables.each do |table|
24
+ ActiveRecord::Base.connection.drop_table(table)
46
25
  end
47
26
  end
48
27
  end
49
28
 
50
- Test::Spec::Should.send(:include, Test::Spec::Rails::ShouldDiffer)
29
+ class BaconFlavour < ActiveRecord::Base
30
+ include AsNewSan
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Fingertips-as_new-san
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eloy Duran
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-09 00:00:00 -07:00
12
+ date: 2009-07-15 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15