public

  • Download Gist
json_shared_templates.rb
Ruby
1 2 3 4 5 6 7 8 9
module JsonSharedTemplates
def _render_template(options)
if [:index, :show].include? options[:action]
render "shared/#{options[:template]}", locals: {prefix: options[:prefixes][0]}
else
super
end
end
end
payment_decorator.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class ApplicationDecorator < Draper::Base
end
 
class PaymentDecorator < ApplicationDecorator
decorates :model, :class => Payment
 
def index_set(json)
json.(model, :id, :some, :brief, :info)
end
 
def show_set(json)
index_set(json)
with_details(json)
end
 
def with_details(json)
json.(model, :big, :data)
end
end
templates
1 2 3 4 5 6 7 8 9
# index.json.jbuilder
json.payments @resources do |json, resource|
PaymentDecorator.decorate(resource).index_set(json)
end
 
# show.json.jbuilder
json.payment do |json|
PaymentDecorator.decorate(@resource).show_set(json)
end
templates_dried
1 2 3 4 5 6 7 8 9
# index.json.jbuilder
json.__send__ prefix, @resources do |json, resource|
Object.const_get("#{prefix.classify}Decorator").decorate(resource).index_set(json)
end
 
# show.json.jbuilder
json.__send__ prefix.singularize do |json|
Object.const_get("#{prefix.classify}Decorator").decorate(@resource).show_set(json)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.