Sha256: 913931d544b4d92a4669429fe97481e0c36c6f1677de9d7ee6664f894771a049

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

require 'sunrise/config/base'

module Sunrise
  module Config
    class Field < Base
      include Sunrise::Utils::EvalHelpers

      # The condition that must be met on an object
      attr_reader :if_condition
      
      # The condition that must *not* be met on an object
      attr_reader :unless_condition
      
      def initialize(abstract_model, parent, options = {})
        super(abstract_model, parent, options)
        
        # Build conditionals
        @if_condition = options.delete(:if)
        @unless_condition = options.delete(:unless)
      end
      
      def visible?(object = nil)
        object.nil? || matches_conditions?(object)
      end
      
      def input_options
        @config_options.dup
      end
      
      def human_name
        @config_options[:label] || abstract_model.model.human_attribute_name(@name)
      end
      
      def html_options
        css = ["padder"]
        css << "grey-but" if input_options[:boolean]
        css << "tags-edit" if association?
        
        opts = { :class => css.join(' ') }
        opts.merge!({ :style => input_options[:div_style] }) if input_options[:div_style]
        opts
      end
      
      def association?
        input_options[:association] === true
      end
      
      def label?
        @config_options[:label] != false
      end

      def nested?
        false
      end
      
      protected
      
        # Verifies that the conditionals for this field evaluate to true for the
        # given object
        def matches_conditions?(object)
          return true if if_condition.nil? && unless_condition.nil?
          
          Array.wrap(if_condition).all? {|condition| evaluate_method(object, condition)} &&
          !Array.wrap(unless_condition).any? {|condition| evaluate_method(object, condition)}
        end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sunrise-cms-0.6.5 lib/sunrise/config/field.rb
sunrise-cms-0.6.4 lib/sunrise/config/field.rb
sunrise-cms-0.6.3 lib/sunrise/config/field.rb
sunrise-cms-0.6.2 lib/sunrise/config/field.rb
sunrise-cms-0.6.1 lib/sunrise/config/field.rb
sunrise-cms-0.6.0 lib/sunrise/config/field.rb
sunrise-cms-0.5.3 lib/sunrise/config/field.rb
sunrise-cms-0.5.2 lib/sunrise/config/field.rb
sunrise-cms-0.5.1 lib/sunrise/config/field.rb
sunrise-cms-0.5.0 lib/sunrise/config/field.rb
sunrise-cms-0.5.0.rc5 lib/sunrise/config/field.rb
sunrise-cms-0.5.0.rc4 lib/sunrise/config/field.rb
sunrise-cms-0.5.0.rc3 lib/sunrise/config/field.rb
sunrise-cms-0.5.0.rc2 lib/sunrise/config/field.rb
sunrise-cms-0.5.0.rc1 lib/sunrise/config/field.rb