Sha256: e7dfd21d78645eae2d9a4be0ed2220aea0a03a3b2ad882fb1388b32d03418542

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Kangaroo
  module Model
    module ReadonlyAttributes
      
      def define_setter attribute_name
        field = fields_hash[attribute_name.to_sym] rescue nil
        
        super and return unless field
        return if field.always_readonly?
        
        unless field.eventually_readonly?
          super
        else
          define_method "#{attribute_name}=" do |value|
            if state.blank? && field.readonly?
              raise Kangaroo::ReadonlyRecord
            elsif field.readonly_in? state
              raise Kangaroo::ReadonlyRecord
            else
              write_attribute attribute_name, value
            end
          end
        end
      end
      
      
      protected
      def attributes_for_update
        without_readonly_attributes super
      end

      def attributes_for_create
        without_readonly_attributes super
      end
      
      def without_readonly_attributes attributes
        attributes.reject do |key, val|
          state && fields_hash[key.to_sym].readonly_in?(state)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kangaroo-0.1.0.alpha1 lib/kangaroo/model/readonly_attributes.rb