-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helper to scrub through yml files, add helper for view_component …
…and sidecar assets
- Loading branch information
1 parent
9dd1554
commit 535fd7b
Showing
5 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
require "classy/yaml/version" | ||
require "classy/yaml/railtie" | ||
require "classy/yaml/engine" | ||
|
||
module Classy | ||
module Yaml | ||
# Your code goes here... | ||
autoload :Helpers, "classy/yaml/helpers" | ||
autoload :ComponentHelpers, "classy/yaml/component_helpers" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Classy | ||
module Yaml | ||
module ComponentHelpers | ||
def yass(*args) | ||
component_name = caller.first.split("/").last.split(".").first | ||
calling_path = caller.first.split("/")[0...-1].join("/") | ||
classy_file = calling_path + "/" + component_name + ".yml" | ||
|
||
helpers.yass(args, classy_files: [classy_file] ) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Classy | ||
module Yaml | ||
class Engine < ::Rails::Engine | ||
paths["lib/classy/yaml"] | ||
config.to_prepare do | ||
ApplicationController.helper(Classy::Yaml::Helpers) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Classy | ||
module Yaml | ||
module Helpers | ||
def yass(*args) | ||
classy_yamls = [] | ||
classy_yamls << YAML.load_file(Rails.root.join("config/utility_classes.yml")) if File.exists?(Rails.root.join("config/utility_classes.yml")) | ||
|
||
classy_files_hash = args.find { |arg| arg.is_a?(Hash) && arg.keys.include?(:classy_files) } | ||
if classy_files_hash.present? | ||
classy_files_hash[:classy_files].each do |file| | ||
if File.exists?(file) && YAML.load_file(file) | ||
file = YAML.load_file(file) | ||
classy_yamls << file if file | ||
end | ||
end | ||
end | ||
|
||
return if classy_yamls.blank? | ||
|
||
keys, classes = flatten_args(values: args) | ||
classes += fetch_classes(keys, classy_yamls: classy_yamls) | ||
|
||
return classes.flatten.join(" ") | ||
end | ||
|
||
private | ||
|
||
def flatten_args(root: [], values: [], keys: [], added_classes: []) | ||
values.each do|value| | ||
if value.is_a?(Hash) | ||
if value.has_key? :add | ||
added_classes << value[:add] | ||
value.except! :add | ||
end | ||
|
||
value.keys.each do |key| | ||
values << (root + [key.to_s]) | ||
flatten_args(root: root + [key.to_s], values: [value[key]], keys: keys, added_classes: added_classes) | ||
end | ||
else | ||
if value.is_a?(Array) | ||
flatten_args(root: root, values: value, keys: keys, added_classes: added_classes) | ||
else | ||
keys << (root + [value.to_s]) | ||
end | ||
end | ||
end | ||
|
||
return keys, added_classes | ||
end | ||
|
||
def fetch_classes(keys, classy_yamls: []) | ||
classes = [] | ||
|
||
keys.map do |key| | ||
base_classes = nil | ||
fetched_classes = nil | ||
|
||
classy_yamls.reverse_each do |classy_yaml| | ||
base_classes ||= if classy_yaml.send(:dig, *key).is_a?(Hash) | ||
classy_yaml.send(:dig, *(key + ['base'])).try(:split, " ") | ||
else | ||
classy_yaml.send(:dig, *(key[0...-1] + ['base'])).try(:split, " ") | ||
end | ||
|
||
fetched_classes ||= unless classy_yaml.send(:dig, *key).is_a?(Hash) | ||
classy_yaml.send(:dig, *key).try(:split, " ") | ||
end | ||
|
||
base_classes = nil if base_classes.blank? | ||
fetched_classes = nil if fetched_classes.blank? | ||
end | ||
|
||
classes << base_classes | ||
classes << fetched_classes | ||
end | ||
|
||
classes.reject!(&:blank?) | ||
return classes.flatten.uniq | ||
end | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.