activerecord-custom_timestamps 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ Custom Timestamps for ActiveRecord 3.x/4.x and Rails 3.x/4.x
2
+ =====
3
+
4
+ Provide a legacy database, you may need to use columns other than created_at/created_on and updated_at/updated_on.
5
+
6
+ It does not change "timestamps" behavior in migrations. New models will continue to get updated_at/created_at or whatever you have designated, nor should it affect existing behavior in Rails (ActiveRecord::Timestamp) that looks for created_at/created_on and updated_at/updated_on and updates those on create/update.
7
+
8
+ It uses the Rails 3.0-4.0 self.record_timestamps to determine if it should update the date of the created_timestamp column(s) and should_record_timestamps? to determine if it should update the date of the updated_timestamp column(s) and it does these in the private create and update methods that then call super to execute the default ActiveRecord::Timestamp defined create and update methods.
9
+
10
+ ### Setup
11
+
12
+ In your Rails 3+ project, add this to your Gemfile:
13
+
14
+ gem 'activerecord-custom_timestamps', :git => 'git://github.com/garysweaver/activerecord-custom_timestamps.git'
15
+
16
+ Then run:
17
+
18
+ bundle install
19
+
20
+ ### Configuration
21
+
22
+ Custom Timestamps lets you do the following in your model to specify which columns should be used for created_at and updated_at:
23
+
24
+ self.created_timestamp = :manufactured_on
25
+ self.updated_timestamp = :modded_on
26
+
27
+ It also supports updating multiple columns.
28
+
29
+ self.created_timestamp = [:manufactured_on, :amalgamated_at]
30
+ self.updated_timestamp = [:redesigned_at, :redesign_release_date]
31
+
32
+ You don't need to specify updated_at or updated_on in self.updated_at, and you don't need to specify created_at or created_on in self.created_at. Those columns are still updated by ActiveRecord (unless you overrode that).
33
+
34
+ ### Usage
35
+
36
+ Just try to save and update an existing model that has the custom timestamp columns.
37
+
38
+ ### License
39
+
40
+ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
41
+
42
+ [lic]: http://github.com/garysweaver/custom_timestamps/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ require 'activerecord-custom_timestamps/version'
2
+ require 'activerecord-custom_timestamps/model'
3
+ require 'activerecord-custom_timestamps/railtie' if defined?(Rails)
4
+
5
+ puts "CustomTimestamps #{CustomTimestamps::VERSION}"
@@ -0,0 +1,41 @@
1
+ module CustomTimestamps
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :created_timestamp, instance_writer: false
7
+ class_attribute :updated_timestamp, instance_writer: false
8
+ end
9
+
10
+ private
11
+
12
+ def create
13
+ if self.created_timestamp && self.record_timestamps
14
+ current_time = current_time_from_proper_timezone
15
+
16
+ Array.wrap(self.created_timestamp).each do |column|
17
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
18
+ write_attribute(column.to_s, current_time)
19
+ end
20
+ end
21
+ end
22
+
23
+ super
24
+ end
25
+
26
+ def update(*args)
27
+ # should_record_timestamps? checks for changes
28
+ if self.updated_timestamp && should_record_timestamps?
29
+ current_time = current_time_from_proper_timezone
30
+
31
+ Array.wrap(self.updated_timestamp).each do |column|
32
+ column = column.to_s
33
+ next if attribute_changed?(column)
34
+ write_attribute(column, current_time)
35
+ end
36
+ end
37
+ super
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ require 'activerecord-custom_timestamps'
2
+
3
+ module CustomTimestamps
4
+ class Railtie < Rails::Railtie
5
+ initializer "custom_timestamps.active_record" do
6
+ ActiveSupport.on_load(:active_record) do
7
+ # ActiveRecord::Base gets new behavior
8
+ include CustomTimestamps::Model # ActiveSupport::Concern
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module CustomTimestamps
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-custom_timestamps
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Gary S. Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: !binary |-
21
+ MA==
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :runtime
32
+ description: Allows sets of custom timestamps for all or some models. Can use different timestamps in migrations.
33
+ email:
34
+ - garysweaver@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - !binary |-
40
+ bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy5yYg==
41
+ - !binary |-
42
+ bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy9tb2RlbC5yYg==
43
+ - !binary |-
44
+ bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy9yYWlsdGllLnJi
45
+ - !binary |-
46
+ bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy92ZXJzaW9uLnJi
47
+ - Rakefile
48
+ - README.md
49
+ homepage: https://github.com/garysweaver/activerecord-custom_timestamps
50
+ licenses:
51
+ - MIT
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: !binary |-
61
+ MA==
62
+ none: false
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: !binary |-
68
+ MA==
69
+ none: false
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.24
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Custom Timestamps for ActiveRecord 3.x/4.x.
76
+ test_files: []
77
+ ...