column_timestamps 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Myron Marston
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ = column_timestamps
2
+
3
+ ActiveRecord manages your updated_at columns for you, updating the timestamp when your records change.
4
+ This gem provides similar, but more granular functionality: in addition to a table-wide updated_at column,
5
+ you can have additional timestamp columns that track when specific columns change.
6
+
7
+ Usage is pretty simple:
8
+ - Create a new datetime column using the naming convention <column>_updated_at.
9
+ - Include ColumnTimestamps in your model.
10
+
11
+ Example:
12
+
13
+ # schema definition...
14
+ ActiveRecord::Schema.define do
15
+ create_table :users, :force => true do |t|
16
+ t.string :email_address
17
+ t.string :name
18
+ t.string :state
19
+ t.datetime :state_updated_at
20
+ t.timestamps
21
+ end
22
+ end
23
+
24
+ # Model...
25
+ class User < ActiveRecord::Base
26
+ include ColumnTimestamps
27
+ end
28
+
29
+ Anytime the state attribute of a user record is changed, the state_updated_at timestamp will be updated to reflect that.
30
+
31
+ == Note on Patches/Pull Requests
32
+
33
+ * Fork the project.
34
+ * Make your feature addition or bug fix.
35
+ * Add tests for it. This is important so I don't break it in a
36
+ future version unintentionally.
37
+ * Commit, do not mess with rakefile, version, or history.
38
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2009 Myron Marston. See LICENSE for details.
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "column_timestamps"
8
+ gem.summary = %Q{Keeps track of when a specific column in an ActiveRecord model changed.}
9
+ gem.description = %Q{Works "automagically" like updated_at...just add a column named <column>_updated_at, and when your column changes, the timestamp will be updated. }
10
+ gem.email = "myron.marston@gmail.com"
11
+ gem.homepage = "http://github.com/myronmarston/column_timestamps"
12
+ gem.authors = ["Myron Marston"]
13
+ gem.add_dependency 'activerecord'
14
+ gem.add_development_dependency 'sqlite3-ruby'
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ gem.add_development_dependency "rr", ">= 0.10.4"
17
+
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ begin
40
+ require 'reek/rake_task'
41
+ Reek::RakeTask.new do |t|
42
+ t.fail_on_error = true
43
+ t.verbose = false
44
+ t.source_files = 'lib/**/*.rb'
45
+ end
46
+ rescue LoadError
47
+ task :reek do
48
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
49
+ end
50
+ end
51
+
52
+ begin
53
+ require 'roodi'
54
+ require 'roodi_task'
55
+ RoodiTask.new do |t|
56
+ t.verbose = false
57
+ end
58
+ rescue LoadError
59
+ task :roodi do
60
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
61
+ end
62
+ end
63
+
64
+ task :default => :spec
65
+
66
+ require 'rake/rdoctask'
67
+ Rake::RDocTask.new do |rdoc|
68
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
69
+
70
+ rdoc.rdoc_dir = 'rdoc'
71
+ rdoc.title = "column_timestamps #{version}"
72
+ rdoc.rdoc_files.include('README*')
73
+ rdoc.rdoc_files.include('lib/**/*.rb')
74
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,24 @@
1
+ require 'activerecord'
2
+
3
+ module ColumnTimestamps
4
+ def self.included(klass)
5
+ klass.send :extend, ClassMethods
6
+ klass.send :before_save, :update_column_timestamps
7
+ end
8
+
9
+ module ClassMethods
10
+ def timestamped_column_names
11
+ @timestamped_column_names ||= self.column_names.select { |c| self.column_names.include?("#{c}_updated_at") }
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def update_column_timestamps
18
+ current_time = self.class.default_timezone == :utc ? Time.now.utc : Time.now
19
+ self.class.timestamped_column_names.each do |column_name|
20
+ self.send("#{column_name}_updated_at=", current_time) if self.send("#{column_name}_changed?")
21
+ end
22
+ true
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include ColumnTimestamps
3
+ end
@@ -0,0 +1,10 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :users, :force => true do |t|
3
+ t.string :email_address
4
+ t.string :name
5
+ t.string :state
6
+ t.datetime :name_updated_at
7
+ t.datetime :state_updated_at
8
+ t.timestamps
9
+ end
10
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'column_timestamps'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ ActiveRecord::Base.establish_connection({ :database => ":memory:", :adapter => 'sqlite3', :timeout => 500 })
9
+ require 'fixtures/schema.rb'
10
+ require 'fixtures/models.rb'
11
+
12
+ Spec::Runner.configure do |config|
13
+ config.mock_with :rr
14
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe User do
4
+ it "should return the timestamped_column_names from #timestamped_column_names" do
5
+ User.timestamped_column_names.sort.should == %w( name state )
6
+ end
7
+
8
+ it "should initially have nil values for the column timestamps" do
9
+ @user = User.new
10
+ @user.name_updated_at.should be_nil
11
+ @user.state_updated_at.should be_nil
12
+ end
13
+
14
+ it "should not set the the column timestamps when saved with name or state values" do
15
+ @user = User.create!
16
+ @user.name_updated_at.should be_nil
17
+ @user.state_updated_at.should be_nil
18
+ end
19
+
20
+ describe "saving the record with a name value" do
21
+ before(:each) do
22
+ @now = @now1 = Time.now
23
+ stub(Time).now { @now }
24
+ @user = User.create!(:name => 'David')
25
+ @user.reload
26
+ end
27
+
28
+ it "should set the name_updated_at timestamp" do
29
+ @user.name_updated_at.to_s.should == @now1.to_s
30
+ end
31
+
32
+ it "should not set the state_updated_at timestamp" do
33
+ @user.state_updated_at.should be_nil
34
+ end
35
+
36
+ describe "and later updating the state value" do
37
+ before(:each) do
38
+ @now = @now2 = (@now1 + 1.day)
39
+ @user.state = 'approved'
40
+ @user.save!
41
+ @user.reload
42
+ end
43
+
44
+ it "should set the state_updated_at timestamp" do
45
+ @user.state_updated_at.to_s.should == @now2.to_s
46
+ end
47
+
48
+ it "should not update the name_updated_at timestamp" do
49
+ @user.name_updated_at.to_s.should == @now1.to_s
50
+ end
51
+
52
+ describe "and later updating another attribute" do
53
+ before(:each) do
54
+ @now = @now3 = (@now2 + 1.day)
55
+ @user.email_address = 'dave@example.com'
56
+ @user.save!
57
+ @user.reload
58
+ end
59
+
60
+ it "should not update the state_updated_at timestamp" do
61
+ @user.state_updated_at.to_s.should == @now2.to_s
62
+ end
63
+
64
+ it "should not update the name_updated_at timestamp" do
65
+ @user.name_updated_at.to_s.should == @now1.to_s
66
+ end
67
+ end
68
+
69
+ describe "and later updating the name attribute" do
70
+ before(:each) do
71
+ @now = @now3 = (@now2 + 1.day)
72
+ @user.name = 'Dave'
73
+ @user.save!
74
+ @user.reload
75
+ end
76
+
77
+ it "should not update the state_updated_at timestamp" do
78
+ @user.state_updated_at.to_s.should == @now2.to_s
79
+ end
80
+
81
+ it "should update the name_updated_at timestamp" do
82
+ @user.name_updated_at.to_s.should == @now3.to_s
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: column_timestamps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Myron Marston
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3-ruby
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.9
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rr
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.4
54
+ version:
55
+ description: "Works \"automagically\" like updated_at...just add a column named <column>_updated_at, and when your column changes, the timestamp will be updated. "
56
+ email: myron.marston@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/column_timestamps.rb
72
+ - spec/fixtures/models.rb
73
+ - spec/fixtures/schema.rb
74
+ - spec/spec.opts
75
+ - spec/spec_helper.rb
76
+ - spec/user_spec.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/myronmarston/column_timestamps
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --charset=UTF-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.5
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Keeps track of when a specific column in an ActiveRecord model changed.
105
+ test_files:
106
+ - spec/fixtures/models.rb
107
+ - spec/fixtures/schema.rb
108
+ - spec/spec_helper.rb
109
+ - spec/user_spec.rb