rails_readonly_injector 0.2.0 → 0.3.0

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: 9509703bc12b066f6af792e478484e61bb0a0249
4
- data.tar.gz: 21225186b776c134d52ec75b95ce3483fd70c21f
3
+ metadata.gz: 25d251e5a12704817f3a4568882e00d68c6d80de
4
+ data.tar.gz: 1b5b6fdbe4cb1772286002fb24ecc22b63e4f5f7
5
5
  SHA512:
6
- metadata.gz: e5aa0948322922fd1940fb817fe4ac52bdcbf3fdc2ff3f1630835e7cd80f4fd4f0bb990812f974b447c2364379f008c5bb951fbf643fd8feb82b5a8fb949c25b
7
- data.tar.gz: 2d7734d7370b5eda65ceababacd75302013dddfd4c26ba2e95fcc4126e75db3c00310198d92a9e9d5d2b9790846f544422a8f67c4f03a4489431886108ec5bb6
6
+ metadata.gz: 9182f0b556f492a5180677208f42833840bc091b6047ce6fbad5fa19ecb491a153575ea68b80f6971526bae58d1a5217be0f03e39b32737aa8fa33f512a5801c
7
+ data.tar.gz: 358b647a57d38f3b0f3235f0b6ad3dc0ee00055384403158a7556446b3b4acaf2b168d93cbfbcb18f2ac9ffaffa4bc197ef365a1c550363fd529ad0409174b8e
@@ -1,3 +1,5 @@
1
+ ## 0.3.0
2
+ - Added ability to explicitly include specific classes.
1
3
  ## 0.2.0
2
4
  - Added ability to exclude specific classes from read-only mode.
3
5
  - Improved support for Rails 5 (Automatic inclusion of descendants of `ApplicationRecord` rather than `ActiveRecord::Base`).
data/README.md CHANGED
@@ -47,7 +47,8 @@ If you want to reset the configuration to the defaults, you can simply call `Rai
47
47
 
48
48
  ## Configuration Options
49
49
  - `read_only` => Whether the site should be in read-only mode. (Default: false)
50
- - `exclude_classes` => An array of classes that should be exempt from read-only mode.
50
+ - `classes_to_exclude` => An array of classes that should be _exempt_ from read-only mode. (Default: `[]`)
51
+ - `classes_to_include` => An array of classes that should be set to read-only mode. (Defaults to `ActiveRecord::Base.descendants` on Rails 3-4, and `ApplicationRecord.descendants` on Rails 5.0+)
51
52
  - `controller_rescue_action` => A lambda expression/Proc to execute when an `ActiveRecord::ReadOnlyRecord` error is raised, from within a controller.
52
53
  ## Development
53
54
 
@@ -3,27 +3,18 @@ require "rails_readonly_injector/configuration"
3
3
 
4
4
  module RailsReadonlyInjector
5
5
  def self.reload!
6
-
7
- Rails.application.eager_load!
8
-
9
- if Rails::VERSION::STRING < '5.0.0'
10
- descendants = ActiveRecord::Base.descendants
11
- else
12
- descendants = ApplicationRecord.descendants
13
- end
14
-
15
- descendants.each do |descendant_class|
6
+ config.classes_to_include.each do |klass|
16
7
 
17
- # Ensure excluded classes aren't set to read-only
18
- if config.classes_to_exclude.include? descendant_class
19
- restore_readonly_method(descendant_class)
8
+ # Ensure we don't impact classes that we want to exclude
9
+ if config.classes_to_exclude.include? klass
10
+ restore_readonly_method(klass)
20
11
  next
21
12
  end
22
13
 
23
14
  if self.config.read_only
24
- override_readonly_method(descendant_class)
15
+ override_readonly_method(klass)
25
16
  else
26
- restore_readonly_method(descendant_class)
17
+ restore_readonly_method(klass)
27
18
  end
28
19
  end
29
20
 
@@ -1,6 +1,6 @@
1
1
  module RailsReadonlyInjector
2
2
  class Configuration
3
- attr_writer :read_only, :classes_to_exclude
3
+ attr_writer :read_only, :classes_to_exclude, :classes_to_include
4
4
 
5
5
  def read_only
6
6
  @read_only || false
@@ -19,6 +19,18 @@ module RailsReadonlyInjector
19
19
  def classes_to_exclude
20
20
  @classes_to_exclude || []
21
21
  end
22
+
23
+ def classes_to_include
24
+ return @classes_to_include if defined? @classes_to_include
25
+
26
+ Rails.application.eager_load!
27
+
28
+ if Rails::VERSION::STRING < '5.0.0'
29
+ ActiveRecord::Base.descendants
30
+ else
31
+ ApplicationRecord.descendants
32
+ end
33
+ end
22
34
  end
23
35
  private_constant :Configuration
24
36
 
@@ -1,3 +1,3 @@
1
1
  module RailsReadonlyInjector
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -28,4 +28,39 @@ RSpec.describe RailsReadonlyInjector.config do
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ describe '#classes_to_include' do
33
+ context 'when not defined' do
34
+ context 'when using Rails < 5.0' do
35
+ before { stub_const('Rails::VERSION::STRING', '4.1.0') }
36
+
37
+ it 'returns the descendants of ActiveRecord::Base' do
38
+ expect(ActiveRecord::Base).to receive(:descendants)
39
+
40
+ RailsReadonlyInjector.config.classes_to_include
41
+ end
42
+
43
+ end
44
+
45
+ context 'when using Rails >= 5.0' do
46
+ before do
47
+ stub_const('Rails::VERSION::STRING', '5.0.0')
48
+
49
+ unless defined? ApplicationRecord
50
+ class ApplicationRecord
51
+ def descendants
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ it 'returns the descendants of ActiveRecord::Base' do
58
+ expect(ApplicationRecord).to receive(:descendants)
59
+
60
+ RailsReadonlyInjector.config.classes_to_include
61
+ end
62
+
63
+ end
64
+ end
65
+ end
31
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_readonly_injector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Walter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-17 00:00:00.000000000 Z
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails