Sha256: 97db15668d86b7f87b6136fdfa513aa1e6141ae2075214e3e93329a0cfdd1a5c

Contents?: true

Size: 1.37 KB

Versions: 9

Compression:

Stored size: 1.37 KB

Contents

require 'active_model'

module Reviewed
  class Base

    include ::Reviewed::Embeddable

    extend ::ActiveModel::Naming

    attr_accessor :attributes

    def initialize(data)
      self.attributes = objectify(data)
    end

    def created_at
      if @attributes.has_key?(:created_at)
        Time.parse(@attributes[:created_at])
      else
        nil
      end
    end

    def updated_at
      if @attributes.has_key?(:updated_at)
        Time.parse(@attributes[:updated_at])
      else
        nil
      end
    end

    def to_s
      "\#<#{self.class.name}:#{self.id}>"
    end

    class << self

      # poor man's polymorphic_url
      def to_path parent_scope=nil
        if parent_scope && parent_scope.respond_to?(:to_param)
          [association_name(parent_scope.class), parent_scope.to_param, association_name].join('/')
        else
          association_name
        end
      end

      def association_name klass=nil
        klass ||= self
        klass.name.demodulize.downcase.pluralize
      end

    end

    def to_path
      [self.class.to_path, self.to_param].join('/')
    end

    def to_param
      id
    end

    def respond_to?(sym, include_private=false)
      super || @attributes.has_key?(sym)
    end

    def method_missing(sym, *args, &block)
      if @attributes.has_key?(sym)
        @attributes[sym]
      else
        super
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
reviewed-0.7.1 lib/reviewed/base.rb
reviewed-0.7 lib/reviewed/base.rb
reviewed-0.6.4 lib/reviewed/base.rb
reviewed-0.6.3 lib/reviewed/base.rb
reviewed-0.6.2 lib/reviewed/base.rb
reviewed-0.6.1 lib/reviewed/base.rb
reviewed-0.6.0 lib/reviewed/base.rb
reviewed-0.5.0 lib/reviewed/base.rb
reviewed-0.4.6 lib/reviewed/base.rb