Created
May 30, 2013 10:52
-
-
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 :|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work... :-)