Sha256: 107c14f23b7066e39d8cba5dfcc89442cfde25ad48cc55abcdfcc685fb1358ae

Contents?: true

Size: 870 Bytes

Versions: 1

Compression:

Stored size: 870 Bytes

Contents

module FeatureRich
  FeatureStruct = Struct.new(:features, :group_features)
  FeatureStruct.class_eval do
    def initialize(f,g)
      self.features = []
      self.group_features = []
      super
    end

    def modify(ary)
      self.features = ary.grep(Symbol)
      self.group_features = ary.grep(GroupFeature){|g| g.name.to_sym }
    end

    def self.default
      new([],[])
    end
  end

  class Feature < ActiveRecord::Base
    belongs_to :featured, :polymorphic => true
    serialize :content, FeatureStruct

    before_save :symbolize_feature_name

    def after_initialize
      self.content ||= FeatureStruct.default
    end

    def modify(ary)
      content_will_change!
      content.modify(ary)
    end

    protected

    def symbolize_feature_name
      content.features.map!(&:to_sym)
      content.group_features.map!(&:to_sym)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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