Skip to content

Commit

Permalink
Merge pull request #14 from Tonksthebear/properly-mix-overwritten-cla…
Browse files Browse the repository at this point in the history
…sses-with-non-overwritten-classes

Properly mix overwritten classes with non overwritten classes
  • Loading branch information
Tonksthebear authored Jun 26, 2024
2 parents b23135b + 33ac07f commit 342db18
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
classy-yaml (1.2)
classy-yaml (1.3.0)
actionpack (>= 6)
activesupport (>= 6)
railties (>= 6)
Expand Down
Binary file removed classy-yaml-1.2.1.gem
Binary file not shown.
Binary file added classy-yaml-1.3.0.gem
Binary file not shown.
7 changes: 7 additions & 0 deletions lib/classy/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ module Yaml
mattr_accessor :default_file
@@default_file = "config/utility_classes.yml"

mattr_accessor :engine_files
@@engine_files = []

mattr_accessor :extra_files
@@extra_files = []

autoload :Helpers, "classy/yaml/helpers"
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
Expand Down
9 changes: 8 additions & 1 deletion lib/classy/yaml/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] }
Expand Down Expand Up @@ -88,7 +96,6 @@ def fetch_classes(keys, classy_yamls: [], skip_base: false)
classes.reject!(&:blank?)
return classes.flatten.uniq
end

end
end
end
2 changes: 1 addition & 1 deletion lib/classy/yaml/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Classy
module Yaml
VERSION = '1.2.1'
VERSION = '1.3.0'
end
end
77 changes: 56 additions & 21 deletions test/classy/yaml_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions test/classy/yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion test/dummy/app/components/application_component.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 15 additions & 2 deletions test/dummy/app/components/test_component.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions test/dummy/app/components/test_component/test_component.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/config/engine_utility_classes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
engine_no_override: "engine-no-override-class"
engine_default_override: "override-this"
engine_extra_override: "override-this"

1 change: 1 addition & 0 deletions test/dummy/config/extra_utility_classes.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
extra_single: "extra-single-class"
engine_extra_override: "extra-overidden-class"
10 changes: 8 additions & 2 deletions test/dummy/config/utility_classes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 342db18

Please sign in to comment.