Sha256: e3558c075dff12d79ebfe4f2be84317b26f3a24475f7c74fe61d2159bf2ade81

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module Cas
  class FormField
    def initialize(section, field_name)
      @section = section
      @field_name = field_name
    end

    def method_missing(name, *args, &block)
      load_field[name]
    end

    private

    def filename
      if Rails.env.test?
        "spec/fixtures/cas.yml"
      else
        Cas::CONFIG_PATH
      end
    end

    def load_field
      @config ||= begin
        begin
          config_file = YAML.safe_load_file(filename, aliases: true)
        rescue NoMethodError, ArgumentError
          config_file = YAML.load_file(filename)
        end
        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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cas-cms-1.0.0.alpha2 lib/cas/form_field.rb
cas-cms-1.0.0.alpha1 lib/cas/form_field.rb