diff --git a/Gemfile.lock b/Gemfile.lock index fbf368c..bd5678c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - classy-yaml (1.2) + classy-yaml (1.3.0) actionpack (>= 6) activesupport (>= 6) railties (>= 6) diff --git a/classy-yaml-1.2.1.gem b/classy-yaml-1.2.1.gem deleted file mode 100644 index e061f47..0000000 Binary files a/classy-yaml-1.2.1.gem and /dev/null differ diff --git a/classy-yaml-1.3.0.gem b/classy-yaml-1.3.0.gem new file mode 100644 index 0000000..8ed3217 Binary files /dev/null and b/classy-yaml-1.3.0.gem differ diff --git a/lib/classy/yaml.rb b/lib/classy/yaml.rb index f6bbb3a..64d8ffc 100644 --- a/lib/classy/yaml.rb +++ b/lib/classy/yaml.rb @@ -6,6 +6,9 @@ module Yaml mattr_accessor :default_file @@default_file = "config/utility_classes.yml" + mattr_accessor :engine_files + @@engine_files = [] + mattr_accessor :extra_files @@extra_files = [] @@ -13,6 +16,10 @@ module Yaml autoload :ComponentHelpers, "classy/yaml/component_helpers" autoload :InvalidKeyError, "classy/yaml/invalid_key_error" + def self.engine_files=(value) + @@engine_files = Array.wrap(value).reject(&:blank?).map { |file| Rails.root.join(file) } + end + def self.extra_files=(value) @@extra_files = Array.wrap(value).reject(&:blank?).map { |file| Rails.root.join(file) } end diff --git a/lib/classy/yaml/helpers.rb b/lib/classy/yaml/helpers.rb index ed247a4..0264846 100644 --- a/lib/classy/yaml/helpers.rb +++ b/lib/classy/yaml/helpers.rb @@ -3,6 +3,14 @@ module Yaml module Helpers def yass(*args) classy_yamls = [] + + Classy::Yaml.engine_files.each do |file| + if File.exist?(file) && YAML.load_file(file) + file = YAML.load_file(file) + classy_yamls << file if file + end + end + classy_yamls << YAML.load_file(Rails.root.join(Classy::Yaml.default_file)) if File.exist?(Rails.root.join(Classy::Yaml.default_file)) classy_files_hash = args.find { |arg| arg.is_a?(Hash) && arg.keys.include?(:classy_files) } || { classy_files: [] } @@ -88,7 +96,6 @@ def fetch_classes(keys, classy_yamls: [], skip_base: false) classes.reject!(&:blank?) return classes.flatten.uniq end - end end end diff --git a/lib/classy/yaml/version.rb b/lib/classy/yaml/version.rb index 5e8b8a6..9125d10 100644 --- a/lib/classy/yaml/version.rb +++ b/lib/classy/yaml/version.rb @@ -1,5 +1,5 @@ module Classy module Yaml - VERSION = '1.2.1' + VERSION = '1.3.0' end end diff --git a/test/classy/yaml_component_test.rb b/test/classy/yaml_component_test.rb index 6308a7a..6efc6e4 100644 --- a/test/classy/yaml_component_test.rb +++ b/test/classy/yaml_component_test.rb @@ -3,77 +3,112 @@ class Classy::YamlComponentTest < ViewComponent::TestCase test "can fetch single utility class" do - render_inline(TestComponent.new classy: :single) + component = TestComponent.new classy: :single + render_inline component assert_text "single-class" + assert_equal component.class_count, 1 end test "can fetch nested utility classes" do - render_inline(TestComponent.new classy: {nested_no_base: :nested}) + component = TestComponent.new classy: {nested_no_base: :nested} + render_inline component assert_text "nested-no-base-class" + assert_equal component.class_count, 1 end test "can fetch multiple nested utility classes" do - render_inline(TestComponent.new(classy: {nested_no_base: [:nested, :nested2]})) + component = TestComponent.new classy: {nested_no_base: [:nested, :nested2]} + render_inline component assert_text "nested-no-base-class nested2-class" + assert_equal component.class_count, 2 end test "includes base if found to nested" do - render_inline(TestComponent.new classy: {nested_base: :nested}) + component = TestComponent.new classy: {nested_base: :nested} + render_inline component assert_text "nested-base-class nested-class" + assert_equal component.class_count, 2 end test "can fetch multiple at same time" do - render_inline(TestComponent.new classy: [:single, nested_no_base: :nested]) + component = TestComponent.new classy: [:single, nested_no_base: :nested] + render_inline component assert_text "single-class nested-no-base-class" + assert_equal component.class_count, 2 end test "can override single utility class" do - render_inline(TestComponent.new classy: :overrideable) + component = TestComponent.new classy: :overrideable + render_inline component assert_text "component-class" + assert_equal component.class_count, 1 end - test "can override nested utility classes" do - render_inline(TestComponent.new classy: {overrideable_nested: :nested}) - assert_text "component-nested-base-class" + test "can override non-base but keep inherited base" do + component = TestComponent.new classy: {overrideable_nested: :nested} + render_inline component + assert_text "overrideable-base-class" assert_text "component-nested-class" + assert_equal component.class_count, 2 + end + + test "can override nested utility classes" do + component = TestComponent.new classy: {overrideable_base_nested: :nested} + render_inline component + assert_text "component-base-nested-base-class" + assert_text "component-base-nested-class" + assert_equal component.class_count, 2 end test "can override multiple nested utility classes" do - render_inline(TestComponent.new(classy: {overrideable_nested: [:nested, :nested2]})) - assert_text "component-nested-base-class" - assert_text "component-nested-class" - assert_text "component-nested2-class" + component = TestComponent.new classy: {overrideable_base_nested: [:nested, :nested2]} + render_inline component + assert_text "component-base-nested-base-class" + assert_text "component-base-nested-class" + assert_text "component-base-nested2-class" + assert_equal component.class_count, 3 end test "can override with original base" do - render_inline(TestComponent.new(classy: {overrideable_no_base_nested: :nested})) + component = TestComponent.new classy: {overrideable_no_base_nested: :nested} + render_inline component assert_text "overrideable-no-base-class" assert_text "component-no-base-nested-class" + assert_equal component.class_count, 2 end test "can find definitions through inherited function call" do - render_inline(TestComponent.new classy: :inherited, inherited: true) + component = TestComponent.new classy: :inherited, inherited: true + render_inline component assert_text "inherited" + assert_equal component.class_count, 1 end test "can find parent component utility classes" do - render_inline(TestComponent::NestedComponent.new classy: :inherited) + component = TestComponent::NestedComponent.new classy: :inherited + render_inline component assert_text "inherited" + assert_equal component.class_count, 1 end test "can find nested component utility classes" do - render_inline(TestComponent::NestedComponent.new classy: :nested) + component = TestComponent::NestedComponent.new classy: :nested + render_inline component assert_text "nested" + assert_equal component.class_count, 1 end test "can find nested component utility classes through inherited function call" do - render_inline(TestComponent::NestedComponent.new classy: :nested_inherited, inherited: true) + component = TestComponent::NestedComponent.new classy: :nested_inherited, inherited: true + render_inline component assert_text "nested-inherited" + assert_equal component.class_count, 1 end test "can skip base" do - render_inline(TestComponent.new classy: {overrideable_nested: :nested, skip_base: true}) - assert_no_text 'component-nested-base-class' - assert_text "component-nested-class" + component = TestComponent.new classy: {overrideable_base_nested: :nested, skip_base: true} + render_inline component + assert_text "component-base-nested-class" + assert_equal component.class_count, 1 end end diff --git a/test/classy/yaml_test.rb b/test/classy/yaml_test.rb index 5e42019..041ca53 100644 --- a/test/classy/yaml_test.rb +++ b/test/classy/yaml_test.rb @@ -74,6 +74,17 @@ class Classy::YamlTest < ActiveSupport::TestCase assert_equal 'extra-single-class', yass(:extra_single) end + test "can add engine utility files that are overridden by default and extra classes" do + Classy::Yaml.setup do |config| + config.engine_files = "config/engine_utility_classes.yml" + config.extra_files = "config/extra_utility_classes.yml" + end + + assert_equal "engine-no-override-class", yass(:engine_no_override) + assert_equal "default-overridden-class", yass(:engine_default_override) + assert_equal 'extra-overidden-class', yass(:engine_extra_override) + end + test "allow skipping of base" do assert_equal "nested-class", yass(nested_base: :nested, skip_base: true) end diff --git a/test/dummy/app/components/application_component.rb b/test/dummy/app/components/application_component.rb index 3b70965..72896b7 100644 --- a/test/dummy/app/components/application_component.rb +++ b/test/dummy/app/components/application_component.rb @@ -1,11 +1,21 @@ class ApplicationComponent < ViewComponent::Base include Classy::Yaml::ComponentHelpers + attr_reader :class_count + def initialize(classy:, inherited: false) @classy = classy @inherited = inherited end + def inherited_classes + @inherited_classes ||= yass(@classy) + end + + def class_count + @class_count ||= @inherited ? inherited_classes&.split(' ')&.length : classes&.split(' ').length + end + def inherited_call - yass(@classy) + inherited_classes end end diff --git a/test/dummy/app/components/test_component.rb b/test/dummy/app/components/test_component.rb index dd8b464..669c6d8 100644 --- a/test/dummy/app/components/test_component.rb +++ b/test/dummy/app/components/test_component.rb @@ -1,11 +1,24 @@ class TestComponent < ApplicationComponent + def before_render + @classes = @inherited ? inherited_call : yass(@classy) + @class_count = @classes.split(' ').length + end + + def classes + @classes ||= @inherited ? inherited_classes : yass(@classy) + end + def call - @inherited ? inherited_call : yass(@classy) + @inherited ? inherited_call : classes end class NestedComponent < TestComponent + def classes + @classes ||= @inherited ? inherited_classes : yass(@classy) + end + def call - @inherited ? inherited_call : yass(@classy) + @inherited ? inherited_call : classes end end end diff --git a/test/dummy/app/components/test_component/test_component.yml b/test/dummy/app/components/test_component/test_component.yml index 9be9bdf..9592f65 100644 --- a/test/dummy/app/components/test_component/test_component.yml +++ b/test/dummy/app/components/test_component/test_component.yml @@ -1,9 +1,12 @@ overrideable: "component-class" overrideable_nested: - base: "component-nested-base-class" nested: "component-nested-class" - nested2: "component-nested2-class" + +overrideable_base_nested: + base: "component-base-nested-base-class" + nested: "component-base-nested-class" + nested2: "component-base-nested2-class" overrideable_no_base_nested: nested: "component-no-base-nested-class" diff --git a/test/dummy/config/engine_utility_classes.yml b/test/dummy/config/engine_utility_classes.yml new file mode 100644 index 0000000..df8e91c --- /dev/null +++ b/test/dummy/config/engine_utility_classes.yml @@ -0,0 +1,4 @@ +engine_no_override: "engine-no-override-class" +engine_default_override: "override-this" +engine_extra_override: "override-this" + diff --git a/test/dummy/config/extra_utility_classes.yml b/test/dummy/config/extra_utility_classes.yml index 23e3116..d0d2bc4 100644 --- a/test/dummy/config/extra_utility_classes.yml +++ b/test/dummy/config/extra_utility_classes.yml @@ -1 +1,2 @@ extra_single: "extra-single-class" +engine_extra_override: "extra-overidden-class" diff --git a/test/dummy/config/utility_classes.yml b/test/dummy/config/utility_classes.yml index dd621e2..3fb4d54 100644 --- a/test/dummy/config/utility_classes.yml +++ b/test/dummy/config/utility_classes.yml @@ -11,10 +11,16 @@ nested_base: overrideable: "override-class" overrideable_nested: - base: "overrideable-nested-base-class" + base: "overrideable-base-class" nested: "overrideable-nested-class" - nested2: "overrideable-nested2-class" + +overrideable_base_nested: + base: "overrideable-base-base-class" + nested: "overrideable-base-nested-class" + nested2: "overrideable-base-nested2-class" overrideable_no_base_nested: base: "overrideable-no-base-class" nested: "overrideable-no-base-nested-class" + +engine_default_override: "default-overridden-class"