activerecord-custom_timestamps 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,6 +23,8 @@ Then run:
23
23
 
24
24
  ### Configuration
25
25
 
26
+ #### In Model
27
+
26
28
  Custom Timestamps lets you do the following in your model to specify which columns should be used for created_at and updated_at:
27
29
 
28
30
  self.created_timestamp = :manufactured_on
@@ -35,6 +37,18 @@ It also supports updating multiple columns.
35
37
 
36
38
  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).
37
39
 
40
+ If you want to control whether your custom self.updated_timestamp column(s) will be updated on create also, and don't want to set that as an application-wide default or use the default, then:
41
+
42
+ self.update_custom_updated_timestamp_on_create = false # default is true
43
+
44
+ #### In Application
45
+
46
+ In environment.rb or wherever seems most appropriate, you can set the configuration option in one of two ways:
47
+
48
+ CustomTimestamps.update_custom_updated_timestamp_on_create = false # default is true
49
+
50
+ Please set this one to either true or false to avoid issues if defaults change later.
51
+
38
52
  ### Usage
39
53
 
40
54
  Just try to save and update an existing model that has the custom timestamp columns.
@@ -43,4 +57,4 @@ Just try to save and update an existing model that has the custom timestamp colu
43
57
 
44
58
  Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
45
59
 
46
- [lic]: http://github.com/garysweaver/custom_timestamps/blob/master/LICENSE
60
+ [lic]: http://github.com/garysweaver/activerecord-custom_timestamps/blob/master/LICENSE
@@ -1,3 +1,4 @@
1
1
  require 'activerecord-custom_timestamps/version'
2
+ require 'activerecord-custom_timestamps/config'
2
3
  require 'activerecord-custom_timestamps/model'
3
4
  require 'activerecord-custom_timestamps/railtie' if defined?(Rails)
@@ -0,0 +1,10 @@
1
+ module CustomTimestamps
2
+ class << self
3
+ attr_accessor :update_custom_updated_timestamp_on_create
4
+ alias_method :update_custom_updated_timestamp_on_create?, :update_custom_updated_timestamp_on_create
5
+ end
6
+ end
7
+
8
+ # defaults
9
+ CustomTimestamps.update_custom_updated_timestamp_on_create = true
10
+
@@ -1,3 +1,5 @@
1
+ require 'activerecord-custom_timestamps/config'
2
+
1
3
  module CustomTimestamps
2
4
  module Model
3
5
  extend ActiveSupport::Concern
@@ -5,17 +7,33 @@ module CustomTimestamps
5
7
  included do
6
8
  class_attribute :created_timestamp, instance_writer: false
7
9
  class_attribute :updated_timestamp, instance_writer: false
10
+ class_attribute :update_custom_updated_timestamp_on_create, instance_writer: false
11
+ puts "now module.object_id=#{CustomTimestamps.object_id} && module.update_custom_updated_timestamp_on_create?=#{CustomTimestamps.update_custom_updated_timestamp_on_create?}"
12
+ self.update_custom_updated_timestamp_on_create = CustomTimestamps.update_custom_updated_timestamp_on_create?
8
13
  end
9
14
 
10
15
  private
11
16
 
12
17
  def create
13
- if self.created_timestamp && self.record_timestamps
18
+ puts "create called. self.created_timestamp=#{self.created_timestamp} && self.record_timestamps=#{self.record_timestamps}"
19
+ if self.record_timestamps
14
20
  current_time = current_time_from_proper_timezone
21
+ if self.created_timestamp
22
+ Array.wrap(self.created_timestamp).each do |column|
23
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
24
+ write_attribute(column.to_s, current_time)
25
+ end
26
+ end
27
+ end
15
28
 
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)
29
+ puts "self.update_custom_updated_timestamp_on_create?=#{self.update_custom_updated_timestamp_on_create?} self.record_timestamps=#{self.record_timestamps} self.updated_timestamp=#{self.updated_timestamp}"
30
+ if self.updated_timestamp && self.update_custom_updated_timestamp_on_create?
31
+ puts "going to write to self.updated_timestamp=#{self.updated_timestamp}"
32
+ Array.wrap(self.updated_timestamp).each do |column|
33
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
34
+ puts "writing updated time #{current_time} to #{column.to_s}"
35
+ write_attribute(column.to_s, current_time)
36
+ end
19
37
  end
20
38
  end
21
39
  end
@@ -24,7 +42,8 @@ module CustomTimestamps
24
42
  end
25
43
 
26
44
  def update(*args)
27
- # should_record_timestamps? checks for changes
45
+ puts "update called"
46
+ # should_record_timestamps? checks for changes, i.e. self.record_timestamps && (!partial_updates? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
28
47
  if self.updated_timestamp && should_record_timestamps?
29
48
  current_time = current_time_from_proper_timezone
30
49
 
@@ -1,3 +1,3 @@
1
1
  module CustomTimestamps
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,34 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-custom_timestamps
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.3
4
+ version: 0.1.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gary S. Weaver
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- version_requirements: !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
19
  - - ! '>='
19
20
  - !ruby/object:Gem::Version
20
- version: !binary |-
21
- MA==
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
22
25
  none: false
23
- requirement: !ruby/object:Gem::Requirement
24
26
  requirements:
25
27
  - - ! '>='
26
28
  - !ruby/object:Gem::Version
27
- version: !binary |-
28
- MA==
29
- none: false
30
- prerelease: false
31
- type: :runtime
29
+ version: '0'
32
30
  description: Allows sets of custom timestamps for all or some models.
33
31
  email:
34
32
  - garysweaver@gmail.com
@@ -36,42 +34,36 @@ executables: []
36
34
  extensions: []
37
35
  extra_rdoc_files: []
38
36
  files:
39
- - !binary |-
40
- bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy5yYg==
41
- - !binary |-
42
- bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy9tb2RlbC5yYg==
43
- - !binary |-
44
- bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy9yYWlsdGllLnJi
45
- - !binary |-
46
- bGliL2FjdGl2ZXJlY29yZC1jdXN0b21fdGltZXN0YW1wcy92ZXJzaW9uLnJi
37
+ - lib/activerecord-custom_timestamps/config.rb
38
+ - lib/activerecord-custom_timestamps/model.rb
39
+ - lib/activerecord-custom_timestamps/railtie.rb
40
+ - lib/activerecord-custom_timestamps/version.rb
41
+ - lib/activerecord-custom_timestamps.rb
47
42
  - Rakefile
48
43
  - README.md
49
44
  homepage: https://github.com/garysweaver/activerecord-custom_timestamps
50
45
  licenses:
51
46
  - MIT
52
- post_install_message:
47
+ post_install_message:
53
48
  rdoc_options: []
54
49
  require_paths:
55
50
  - lib
56
51
  required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
57
53
  requirements:
58
54
  - - ! '>='
59
55
  - !ruby/object:Gem::Version
60
- version: !binary |-
61
- MA==
62
- none: false
56
+ version: '0'
63
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
64
59
  requirements:
65
60
  - - ! '>='
66
61
  - !ruby/object:Gem::Version
67
- version: !binary |-
68
- MA==
69
- none: false
62
+ version: '0'
70
63
  requirements: []
71
- rubyforge_project:
64
+ rubyforge_project:
72
65
  rubygems_version: 1.8.24
73
- signing_key:
66
+ signing_key:
74
67
  specification_version: 3
75
68
  summary: Custom Timestamps for ActiveRecord 3.x/4.x.
76
69
  test_files: []
77
- ...