lib/cas/section_config.rb in cas-cms-0.1.3 vs lib/cas/section_config.rb in cas-cms-1.0.0.alpha1
- old
+ new
@@ -3,30 +3,16 @@
def initialize(section)
@section = section
end
def list_order_by
- config = YAML.load_file(filename)
- sites = config["sites"]
- site = sites[@section.site.slug]
- section = site["sections"]
-
- order_field = section.find { |key, value|
- key == @section.slug
- }[1]['list_order_by']
-
+ order_field = load_section_config[1]['list_order_by']
order_field || ['created_at']
end
def list_fields
- config = YAML.load_file(filename)
- sites = config["sites"]
- site = sites[@section.site.slug]
- section = site["sections"]
- fields = section.find { |key, value|
- key == @section.slug
- }[1]["list_fields"]
+ fields = load_section_config[1]["list_fields"]
fields || ['title', 'created_at']
end
def accessible_by_user?(user)
roles = user.roles.map(&:to_s)
@@ -38,17 +24,11 @@
true
end
end
def form_has_field?(field)
- config = YAML.load_file(filename)
- sites = config["sites"]
- site = sites[@section.site.slug]
- section = site["sections"]
- section_fields = section.find { |key, value|
- key == @section.slug
- }[1]["fields"]
+ section_fields = load_section_config[1]["fields"]
Array.wrap(section_fields).any? do |section_field|
if section_field.is_a?(Hash)
section_field.keys.map(&:to_s).include?(field.to_s)
else
@@ -61,24 +41,38 @@
def filename
if Rails.env.test?
"spec/fixtures/cas.yml"
else
- "cas.yml"
+ Cas::CONFIG_PATH
end
end
def load_field
@config ||= begin
- config_file = YAML.load_file(filename)
- sites = config_file["sites"]
- site = sites[@section.site.slug]
- section = site["sections"]
- field = section.find { |key, value|
- key == @section.slug
- }
+ field = load_section_config
(field && field[1]) || {}
end
+ end
+
+ def load_section_config
+ begin
+ config = YAML.safe_load_file(filename, aliases: true)
+ rescue NoMethodError, ArgumentError
+ config = YAML.load_file(filename)
+ end
+
+ sites = config["sites"]
+ site = sites[@section.site.slug]
+
+ if site.blank?
+ raise(
+ Cas::Exceptions::UndefinedSite,
+ "Site #{@section.site.slug} is undefined in the #{filename} file."
+ )
+ end
+
+ site["sections"].find { |key, value| key == @section.slug }
end
end
end