engine/app/presenters/locomotive/base_presenter.rb

42 lines
998 B
Ruby
Raw Normal View History

class Locomotive::BasePresenter
include ActionView::Helpers::SanitizeHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
include ActionView::Helpers::TextHelper
include ActionView::Helpers::NumberHelper
attr_reader :source, :options, :ability
delegate :created_at, :updated_at, :to => :source
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
end
def id
self.source.persisted? || self.source.embedded? ? self.source._id.to_s : nil
end
def ability?
self.ability.present?
end
def included_methods
%w(id created_at updated_at)
end
def as_json(methods = nil)
methods ||= self.included_methods
{}.tap do |hash|
methods.each do |meth|
hash[meth] = self.send(meth.to_sym) rescue nil
end
end
end
end