Skip to content

Commit f0b1a7f

Browse files
authored
Make inheritable options configurable (#68)
* make inheritable options a configurable thing * assign
1 parent 58a894e commit f0b1a7f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

lib/subroutine.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ def self.include_defaults_in_params?
2525
false
2626
end
2727

28+
def self.inheritable_field_options=(opts)
29+
@inheritable_field_options = opts.map(&:to_sym)
30+
end
31+
32+
def self.inheritable_field_options
33+
@inheritable_field_options ||= %i[mass_assignable field_reader field_writer groups aka]
34+
end
35+
2836
end

lib/subroutine/fields/configuration.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Fields
55
class Configuration < ::SimpleDelegator
66

77
PROTECTED_GROUP_IDENTIFIERS = %i[all original default].freeze
8-
INHERITABLE_OPTIONS = %i[mass_assignable field_reader field_writer groups aka].freeze
98
NO_GROUPS = [].freeze
109

1110
def self.from(field_name, options)
@@ -61,7 +60,7 @@ def get_default
6160
end
6261

6362
def inheritable_options
64-
config.slice(*INHERITABLE_OPTIONS)
63+
config.slice(*Subroutine.inheritable_field_options)
6564
end
6665

6766
def mass_assignable?

lib/subroutine/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Subroutine
44

55
MAJOR = 4
66
MINOR = 1
7-
PATCH = 1
7+
PATCH = 3
88
PRE = nil
99

1010
VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")

test/subroutine/association_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,20 @@ def test_find_by_is_used_if_raise_on_miss_is_false
345345
assert_nil op.user
346346
end
347347

348+
def test_inheritable_options_are_inherited_by_child_fields
349+
Subroutine.stubs(:inheritable_field_options).returns(%i[foo bar])
350+
klass = Class.new(OpWithAssociation) do
351+
association :buster, foo: true, bar: false, baz: true
352+
association :mister, polymorphic: true, foo: true, bar: false, baz: true
353+
end
354+
355+
%i[buster_id mister_id mister_type].each do |field_name|
356+
field = klass.field_configurations.fetch(field_name)
357+
assert_equal true, field[:foo]
358+
assert_equal false, field[:bar]
359+
assert_equal false, field.config.key?(:baz)
360+
end
361+
end
362+
348363
end
349364
end

0 commit comments

Comments
 (0)