2011-11-16 00:39:16 +00:00
|
|
|
class Locomotive::BasePresenter
|
|
|
|
|
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2012-01-09 14:49:59 +00:00
|
|
|
extend ActionView::Helpers::SanitizeHelper::ClassMethods
|
2011-11-16 00:39:16 +00:00
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
attr_reader :source, :options, :ability
|
2011-11-16 00:39:16 +00:00
|
|
|
|
2011-11-21 01:27:05 +00:00
|
|
|
delegate :created_at, :updated_at, :to => :source
|
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
def initialize(object, options = {})
|
|
|
|
@source = object
|
|
|
|
@options = options
|
|
|
|
|
|
|
|
if @options && @options[:current_account] && @options[:current_site]
|
|
|
|
@ability = Locomotive::Ability.new @options[:current_account], @options[:current_site]
|
|
|
|
end
|
2011-11-16 00:39:16 +00:00
|
|
|
end
|
|
|
|
|
2011-11-19 14:47:56 +00:00
|
|
|
def id
|
2012-01-02 13:54:01 +00:00
|
|
|
self.source.persisted? || self.source.embedded? ? self.source._id.to_s : nil
|
2011-11-19 14:47:56 +00:00
|
|
|
end
|
|
|
|
|
2011-11-29 13:58:19 +00:00
|
|
|
def ability?
|
|
|
|
self.ability.present?
|
|
|
|
end
|
|
|
|
|
2011-11-21 01:27:05 +00:00
|
|
|
def included_methods
|
|
|
|
%w(id created_at updated_at)
|
|
|
|
end
|
|
|
|
|
2011-12-08 15:47:17 +00:00
|
|
|
def as_json(methods = nil)
|
|
|
|
methods ||= self.included_methods
|
2011-11-21 01:27:05 +00:00
|
|
|
{}.tap do |hash|
|
2012-01-09 14:49:59 +00:00
|
|
|
methods.each do |meth|
|
|
|
|
hash[meth] = self.send(meth.to_sym) rescue nil
|
2011-11-21 01:27:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-16 00:39:16 +00:00
|
|
|
|
|
|
|
end
|