Sha256: 026d3057cfed9d2f42b785fb5c2c6c70f34574df8166ac67073c99ccf262c96a

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module FeatureRich
  module ModelBehaviour

    def self.included(base)
      base.extend ClassMethods
    end # self.included

    module ClassMethods
      def has_feature
        has_one :feature, :as => :featured, :class_name => "FeatureRich::Feature", :autosave => true
        include InstanceMethods
      end
    end # ClassMethods

    module InstanceMethods

      def features
        _feature.content
      end

      def features=(ary)
        _feature.modify(ary)
      end

      def has_feature?(feature, options = {})
        with_group(feature, options[:group]) do |f|
          case f
          when Symbol, String
            features.features.include?(f.to_sym)
          when GroupFeature
            f.disabled? || f.subset?(features.features) || features.group_features.include?(f.name)
          when FeatureHandler
            f.disabled? || features.features.include?(f.name)
          end
        end
      end

      protected

      def _feature
        (feature || build_feature(:content => FeatureStruct.default))
      end

      def with_group(fname, with)
        if with
          feature = FeatureRich::Engine.groups[fname]
        else
          feature = FeatureRich::Engine.features.find{|f| f.name == fname} || fname
        end
        yield(feature) || false
      end

    end # InstanceMethods

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
feature-rich-0.2.0 lib/feature-rich/model_behaviour.rb