deep_freezer 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 634689567ca196953f2b6a1d45d00d1c1cb71877a973f1396584424ae4278719
4
- data.tar.gz: 69c5ec1368df575495821a55034500a86bf461483df18c5edda9cf5ef35761eb
3
+ metadata.gz: 5da5f1023b348198c41593af730f028bab393e9a3608a9b2fda5719f4e60b868
4
+ data.tar.gz: 397bd7359940f94439609de9c757cca26ca53037f3c738b41d6f056b66ee4163
5
5
  SHA512:
6
- metadata.gz: b58d13ef0aa7c7082ebbd5ccb428afe9b246077b77c8882620d2c49f9e5624228c76589017b2ce2dde6a63de7fd31a89a8e66ec46c180c36af940389618d92eb
7
- data.tar.gz: 1606d95af4b5dbccfd2c719d9231c5d056d5468fa238ea536db1c74500fe75204cdc2ec924fed2e1c5cf656181f45d4f58631e00b925833ba44c26be1c6c05b4
6
+ metadata.gz: 7fa198b5b8a2c099a878fffed3d264bdbe15023eb11e904f6457a50374c66916f2b5ad8b8a82e9e03a40775b91c7f11cde4ec67348e278f1b1f8b9f3c1e25977
7
+ data.tar.gz: e7b80fc015c9650bef38d56ebe46d201c89bfdfbafbb05d04ceb2128a056f5b5a374ef2f849c9105889bb4156ccba2b16e8597d8448ac4f229cdf8667d9955c6
data/README.md CHANGED
@@ -22,25 +22,33 @@ And then execute:
22
22
 
23
23
  Or install it yourself as:
24
24
 
25
- $ gem install freezer
25
+ $ gem install deep_freezer
26
+
27
+ Requires Ruby 3.1+ and ActiveRecord 7.1+.
26
28
 
27
29
  ## Usage
28
30
 
29
31
  ### Config
30
32
  Be sure to load your freezer classes in `development.rb`
31
33
  ```
32
- Dir.glob(Rails.root.join("lib", "freezers", "*.rb")).each do |file|
34
+ Dir.glob(Rails.root.join("lib", "freezers", "**", "*.rb")).each do |file|
33
35
  require file
34
36
  end
35
37
  ```
36
38
 
37
39
  Create an initializer and set the path for fixtures to be saved
38
40
 
39
- `Freezer::Base.fixture_path = Rails.root.join("db", "seeds")`
41
+ `DeepFreezer::Base.fixture_path = Rails.root.join("db", "seeds")`
40
42
 
41
43
  ### Define Freezers
42
44
 
43
- Define a `DeepFreezer` for your model
45
+ Generate a freezer for a model:
46
+
47
+ $ rails g freezer Post
48
+
49
+ This creates `lib/freezers/post_freezer.rb` with every column of the model (it needs a database connection). Pass attributes to freeze only those, for example `rails g freezer Post title body`. Namespaced models go in matching subdirectories: `rails g freezer Admin::User` creates `lib/freezers/admin/user_freezer.rb`.
50
+
51
+ Or define a `DeepFreezer` for your model by hand:
44
52
 
45
53
  ```
46
54
  class PostFreezer < DeepFreezer::Base
@@ -87,7 +95,7 @@ This will result in a `posts.yml` file in `db/seeds` which can be loaded by addi
87
95
 
88
96
  ### Reset Fixtures
89
97
 
90
- Fixtures can be deleted manually in the directory, or by running `Freezers::Base.reset!`
98
+ Fixtures can be deleted manually in the directory, or by running `DeepFreezer::Base.reset!`, which also clears subdirectories used by namespaced models.
91
99
 
92
100
  ## Development
93
101
 
@@ -114,4 +122,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
114
122
 
115
123
  ## Code of Conduct
116
124
 
117
- Everyone interacting in the Freezer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/markprovan/deep_freezer/blob/master/CODE_OF_CONDUCT.md).
125
+ Everyone interacting in the DeepFreezer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/markprovan/deep_freezer/blob/master/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeepFreezer
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.0"
5
5
  end
@@ -0,0 +1,13 @@
1
+ Description:
2
+ Creates a freezer class for a model in lib/freezers.
3
+
4
+ With no attributes, every column of the model is frozen. This needs a
5
+ database connection. Pass attributes to freeze only those instead.
6
+
7
+ Example:
8
+ rails g freezer User
9
+ rails g freezer User name email
10
+ rails g freezer Admin::User
11
+
12
+ creates lib/freezers/user_freezer.rb
13
+ creates lib/freezers/admin/user_freezer.rb
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ # Generates a freezer for a model:
6
+ #
7
+ # rails g freezer User
8
+ #
9
+ # See USAGE for details.
10
+ class FreezerGenerator < Rails::Generators::NamedBase
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ argument :attributes, type: :array, default: [], banner: "attribute attribute"
14
+
15
+ def create_freezer_file
16
+ # Resolve the attributes first: Thor creates the file before rendering the
17
+ # template, so a failure while rendering would leave an empty file behind.
18
+ freezer_attributes
19
+
20
+ template "freezer.rb.tt", File.join("lib/freezers", class_path, "#{file_name}_freezer.rb")
21
+ end
22
+
23
+ private
24
+
25
+ def freezer_class_name
26
+ "#{class_name}Freezer"
27
+ end
28
+
29
+ # Explicit attributes win; otherwise every column of the model.
30
+ def freezer_attributes
31
+ @freezer_attributes ||= attributes.empty? ? model_columns : attributes.map(&:name)
32
+ end
33
+
34
+ def model_columns
35
+ model = class_name.safe_constantize
36
+ unless model.is_a?(Class) && model < ActiveRecord::Base
37
+ raise Thor::Error, "Could not find the model #{class_name}. " \
38
+ "Pass the attributes to freeze explicitly: rails g freezer #{class_name} name email"
39
+ end
40
+
41
+ model.column_names
42
+ rescue ActiveRecord::ActiveRecordError => e
43
+ raise Thor::Error, "Could not read the columns of #{class_name} (#{e.message}). " \
44
+ "Pass the attributes to freeze explicitly: rails g freezer #{class_name} name email"
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ # Review the attributes below. Anything personal or secret should be overridden
2
+ # with a method of the same name, for example:
3
+ #
4
+ # def email
5
+ # "user#{id}@example.com"
6
+ # end
7
+ class <%= freezer_class_name %> < DeepFreezer::Base
8
+ <% freezer_attributes.each_with_index do |attribute, index| -%>
9
+ <%= index.zero? ? "freeze" : " " %> :<%= attribute %><%= "," unless index == freezer_attributes.size - 1 %>
10
+ <% end -%>
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_freezer
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Provan
@@ -43,6 +43,20 @@ dependencies:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: railties
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '7.1'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '7.1'
46
60
  - !ruby/object:Gem::Dependency
47
61
  name: rake
48
62
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +128,9 @@ files:
114
128
  - lib/deep_freezer/base.rb
115
129
  - lib/deep_freezer/defrost.rb
116
130
  - lib/deep_freezer/version.rb
131
+ - lib/generators/freezer/USAGE
132
+ - lib/generators/freezer/freezer_generator.rb
133
+ - lib/generators/freezer/templates/freezer.rb.tt
117
134
  homepage: https://github.com/markprovan/deep_freezer
118
135
  licenses:
119
136
  - MIT