Sha256: 93da644b490fa41a9a4de8c837c6d0248405d99338ba5b439ea01bb7584c2f38

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module DecorateResource
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def decorate_resource
      before_filter :decorate_resource
    end
  end

  protected

  def resource_name
    self.class.name.demodulize.gsub(/Controller/, "").singularize
  end

  def resource_instance_variable_name
    if current_action_type == :member
      "@#{resource_name.constantize.name.underscore}"
    else
      "@#{resource_name.constantize.name.pluralize.underscore}"
    end
  end

  def current_action_type
    if (params.include?(:id) || [:new, :create, :edit, :update, :destroy].include?(params[:action].to_sym)) && !([:index].include? params[:action].to_sym)
      :member
    else
      :collection
    end
  end

  def decorate_resource
    records = instance_variable_get resource_instance_variable_name
    decorated_records = "#{resource_name}Decorator".constantize.decorate records
    instance_variable_set resource_instance_variable_name, decorated_records
  end
end


if defined? ActionController::Base
  ActionController::Base.class_eval do
    include DecorateResource
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decorate_resource-1.0.0 lib/decorate_resource.rb