Sha256: 3bc4cd06831d6345a9d66c3ff78bffd63a9a476087de7d15ea9cb36bbce38747

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

module Cas
  class SectionConfig
    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 || ['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 || ['title', 'created_at']
    end

    def accessible_by_user?(user)
      roles = user.roles.map(&:to_s)
      accessible_roles = load_field["accessible_roles"]

      if accessible_roles.present?
        (accessible_roles.map(&:to_s) & roles).compact.present?
      else
        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"]

      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
          section_field.to_s == field.to_s
        end
      end
    end

    private

    def filename
      if Rails.env.test?
        "spec/fixtures/cas.yml"
      else
        "cas.yml"
      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 && field[1]) || {}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cas-cms-0.1.3 lib/cas/section_config.rb
cas-cms-0.1.2 lib/cas/section_config.rb
cas-cms-0.1.1 lib/cas/section_config.rb
cas-cms-0.1.0 lib/cas/section_config.rb