module Eitil module ControllerDecorator extend ActiveSupport::Concern included do private def decorate(dec_item, dec_method: nil, dec_class: nil, **dec_kwargs) all_args_to_ivars binding set_ivars :dec_class, :dec_method, :decorator send_to_decorator end def send_to_decorator @dec_kwargs.any? ? @decorator.send(@dec_method, @dec_item, @dec_kwargs) : @decorator.send(@dec_method, @dec_item) rescue NoMethodError => e inform_no_method_for_decorator_error @dec_item end def inform_no_method_for_decorator_error message = "Warning: NoMethodError for #{@dec_class}.#{@dec_method}, returned @dec_item instead." Logger.new("#{Rails.root}/log/decorator_log.log").warn message warn message end def set_dec_method @dec_method = @dec_method || derived_dec_method || :base end def derived_dec_method return unless respond_to? :params return :app if params["isMobile"] return :web if params["isWeb"] end def set_dec_class @dec_class = @dec_class ? manual_set_dec_class : derived_dec_class end def manual_set_dec_class "#{@dec_class}Decorator".constantize end def derived_dec_class "#{@dec_item.class.name}Decorator".constantize end def set_decorator @decorator = @dec_class.new controller_ivars end def controller_ivars eval(Eitil.get_controller_ivars_method).map do |ivar| { ivar => instance_variable_get("@#{ivar.to_s}") } end.inject &:merge end end end end