class Cas::FormField

Public Class Methods

new(section, field_name) click to toggle source
# File lib/cas/form_field.rb, line 3
def initialize(section, field_name)
  @section = section
  @field_name = field_name
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/cas/form_field.rb, line 8
def method_missing(name, *args, &block)
  load_field[name]
end

Private Instance Methods

filename() click to toggle source
# File lib/cas/form_field.rb, line 14
def filename
  if Rails.env.test?
    "spec/fixtures/cas.yml"
  else
    "cas.yml"
  end
end
load_field() click to toggle source
# File lib/cas/form_field.rb, line 22
def load_field
  @config ||= begin
    config_file = YAML.load_file(filename)
    sites = config_file["sites"]
    site = sites[@section.site.slug]
    section = site["sections"]
    fields = section.find { |key, value|
      key == @section.slug
    }[1]["fields"]

    config = fields.find do |field|
      if field.is_a?(Hash)
        field[@field_name.to_s]
      else
        field == @field_name.to_s
      end
    end

    if config.is_a?(Hash)
      config = config[@field_name.to_s]
      config = config.deep_symbolize_keys
    else
      config = {}
    end

    config.merge(
      format: (config[:format] || [:day, :month, :year]).map(&:to_sym)
    )
  end
end