Skip to content

Instantly share code, notes, and snippets.

@victorcreed
Created May 30, 2013 10:52
Show Gist options
  • Save victorcreed/5677075 to your computer and use it in GitHub Desktop.
Save victorcreed/5677075 to your computer and use it in GitHub Desktop.
another stupid test with draper gem, metaprograming. I hate to decorate object every time so i wrote this.. i will use this till i find out its a bad pratice. is it bad practice ... .still wonder :|
lib/oleku/presentation.rb
-----------------------------------------------
module Oleku
module Presentation
module Video
def self.included base
base.send :include, InstanceMethods
end
module InstanceMethods
def method_missing( meth, *args, &block)
if meth.to_s =~ /decorate_/
self.decorate.send(meth.to_s.gsub("decorate_", ""))
else
super
end
end
end
end
end
end
-------------------------------------------------------------------------------
app/decorators/video_decorator.rb
-------------------------------------------------------------------------------
class VideoDecorator < Draper::Decorator
include Draper::LazyHelpers
delegate_all
%w(rent buy).each do |type|
VideoDecorator.class_eval do
define_method "#{type}_box".to_sym do
h.content_tag :div, class: "package-detail" do
"#{send("#{type}_package_to", (current_user.try(:currency_format) || "usd")).to_f.round(2)} #{( (current_user.try(:currency_format) || "usd").try(:upcase) )}"
end
end
end
end
end
-----------------------------------------------------------------------------------
app/models/video.rb
-----------------------------------------------------------------------------------
class Video < ActiveRecord::Base
# ---- alot of crap code
include Oleku::Presentation::Video
# ---- alot of crap code
end
@ilatif
Copy link

ilatif commented Sep 10, 2013

Nice work... :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment