<%if @category.image_url%>
<%= link_to image_tag("/images/#{@category.id}.png") + "some extra text", category_path(@category), class: 'class-name', remote: true %>
<%else%>
<%= link_to image_tag("/images/default.png") + "some extra text", category_path(@category), class: 'class-name', remote: true %>
<%end%>
# app/presenters/category_presenter.rb
class CategoryPresenter < BasePresenter
presents :category
def featured_image
image_path = category.image_url ? category.id : "default"
link_to image_tag("/images/#{image_path}.png") + "some extra text", category_path(category), :class => 'class-name', :remote => true
end
def top_brands
@top_brands ||= category.top_brands
end
def top_keywords
@top_keywords ||= category.top_keywords
end
end
class BasePresenter
def initialize(object, template)
@object = object
@template = template
end
def self.presents(name)
define_method(name) do
@object
end
end
def method_missing?(*args, &block)
@template.send(*args, &block)
end
end
# app/views/category/index.html
<% present @category do |category_presenter|%>
<%= category_presenter.featured_image%>
<% end %>
# app/helpers/application_helper.rb
def present(object, klass = nil)
klass ||= "#{object.class}Presenter".constantize
presenter = object.new(object, self)
yield(presenter)
end
# To access presenters from controller
# app/controllers/application_controller.rb
private
def present(object, klass = nil)
klass ||= "#{object.class}Presenter".constantize
klass.new(object, view_context)
end