activerecord-userstamp 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6113fc9e7a0eec6ad0ebe4f883c2b07d0f10d3f2
4
- data.tar.gz: 481179a5004384a5f8173cd04efaf69f7bee1e37
3
+ metadata.gz: 7a7867df9eb9d4eea12bdad4a66da1c5412e0593
4
+ data.tar.gz: b888a7d64f3f13306a2e0cf371775d3e99ba7aba
5
5
  SHA512:
6
- metadata.gz: 4a0260351476dde1e1893b9182bff0c19eb8118c9fa5d30db8b1be28a22dcdce1ea4a7a346e39bd0ff81a42e91a1a44b29e4c2f5d1ccacba485f12b0451f2d33
7
- data.tar.gz: 8661e0637d5ee7231ac426243f9b1ceec0e03b42ac980d269462f243b054feaadb3717a7c3d2dc5eca5e0ae429bd436215d7c1d0f7c8237c0debf363a3711d1e
6
+ metadata.gz: 91875d1531de92434925a3408e7da73bd84b82a56490d4687409dd68c4169b33aa3e0e616a10f225829fd2257b42e61cfb1cc5a5b2d8957d4c645617bbec5555
7
+ data.tar.gz: d11f8898d2a39b569a76e9e124f78036a4dbcd2d46f396e1df94256d4861a31232df35b3d5e0cde13249d7a693d3ea1e32628eba13b4978d41f950fd7384da05
data/.gitignore CHANGED
@@ -2,4 +2,3 @@
2
2
  *.sqlite3
3
3
  /.rdoc
4
4
  /spec/examples.txt
5
- /.bundle
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # Changelog
2
+ ## 3.0.1 (11-7-2015)
3
+ * Joel Low - Only declare the creator/updater/deleter associations when the table has
4
+ the attribute columns. If the columns cannot be determined (e.g. if the
5
+ table has not been defined, such as during tests), then the model would
6
+ need to explicitly call `stampable`.
2
7
 
3
8
  ## 3.0.0 (10-7-2015)
4
9
  * Joel Low - Remove `compatibility_mode`. Use the `creator_attribute`,
data/README.md CHANGED
@@ -27,7 +27,7 @@ In addition to these, I have cherry picked ideas and changes from the following
27
27
  - [konvenit](https://github.com/konvenit/userstamp)
28
28
 
29
29
  Finally, this gem only supports Ruby 2.0 and above. Yes, you really should upgrade to a supported
30
- version of Ruby.
30
+ version of Ruby. This gem is tested only on Rails 4.2; but it should work with Rails 4+.
31
31
 
32
32
  ## Features
33
33
  ### Soft-deletes
@@ -57,7 +57,8 @@ Do create a ticket if it is broken, with a pull-request if possible.
57
57
  While examining the userstamp gem's network on Github, it was noticed that quite a few forks were
58
58
  made to allow customisability in the name and type of the column with the database migration.
59
59
 
60
- This gem now supports customised column names.
60
+ This gem now supports customised column names. See the [usage](#usage) section on the
61
+ configuration options supported.
61
62
 
62
63
  ### Saving before validation
63
64
  This fork includes changes to perform model stamping before validation. This allows models to
@@ -68,21 +68,23 @@ module ActiveRecord::Userstamp::Stampable
68
68
 
69
69
  # Defines the associations for Userstamp.
70
70
  def add_userstamp_associations(options)
71
- config = ActiveRecord::Userstamp.config
72
- klass = stamper_class.try(:name)
73
- relation_options = options.reverse_merge(class_name: klass)
74
-
75
71
  ActiveRecord::Userstamp::Utilities.remove_association(self, :creator)
76
72
  ActiveRecord::Userstamp::Utilities.remove_association(self, :updater)
77
73
  ActiveRecord::Userstamp::Utilities.remove_association(self, :deleter)
78
74
 
79
- belongs_to :creator, relation_options.reverse_merge(foreign_key: config.creator_attribute)
80
- belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute)
75
+ associations = ActiveRecord::Userstamp::Utilities.available_association_columns(self)
76
+ return if associations.nil?
81
77
 
82
- if config.deleter_attribute
83
- remove_method(:deleter) if method_defined?(:deleter)
84
- belongs_to :deleter, relation_options.reverse_merge(foreign_key: config.deleter_attribute)
85
- end
78
+ config = ActiveRecord::Userstamp.config
79
+ klass = stamper_class.try(:name)
80
+ relation_options = options.reverse_merge(class_name: klass)
81
+
82
+ belongs_to :creator, relation_options.reverse_merge(foreign_key: config.creator_attribute) if
83
+ associations.first
84
+ belongs_to :updater, relation_options.reverse_merge(foreign_key: config.updater_attribute) if
85
+ associations.second
86
+ belongs_to :deleter, relation_options.reverse_merge(foreign_key: config.deleter_attribute) if
87
+ associations.third
86
88
  end
87
89
  end
88
90
 
@@ -1,4 +1,9 @@
1
1
  module ActiveRecord::Userstamp::Utilities
2
+ # Removes the association methods from the model.
3
+ #
4
+ # @param [Class] model The model to remove methods from.
5
+ # @param [Symbol] association The name of the association to remove.
6
+ # @return [void]
2
7
  def self.remove_association(model, association)
3
8
  methods = [
4
9
  association,
@@ -14,5 +19,22 @@ module ActiveRecord::Userstamp::Utilities
14
19
  end
15
20
  end
16
21
  end
22
+
23
+ # Obtains the creator/updater/deleter columns which are present in the model.
24
+ #
25
+ # @param [Class] model The model to query.
26
+ # @return [nil|Array<(bool, bool, bool)>] Nil if the model does not have a table defined.
27
+ # Otherwise, a tuple of booleans indicating the presence of the created, updated, and deleted
28
+ # columns.
29
+ def self.available_association_columns(model)
30
+ columns = Set[*model.column_names]
31
+ config = ActiveRecord::Userstamp.config
32
+
33
+ [config.creator_attribute.present? && columns.include?(config.creator_attribute.to_s),
34
+ config.updater_attribute.present? && columns.include?(config.updater_attribute.to_s),
35
+ config.deleter_attribute.present? && columns.include?(config.deleter_attribute.to_s)]
36
+ rescue ActiveRecord::StatementInvalid => _
37
+ nil
38
+ end
17
39
  end
18
40
 
@@ -1,4 +1,4 @@
1
1
  module ActiveRecord; end
2
2
  module ActiveRecord::Userstamp
3
- VERSION = '3.0.0'
3
+ VERSION = '3.0.1'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-userstamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Low
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-10 00:00:00.000000000 Z
11
+ date: 2015-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport