Module: Fiveruns::Dash::ActiveRecordContext
- Defined in:
- lib/fiveruns/dash/recipes/activerecord.rb
Constant Summary collapse
- CLASS_METHODS =
%w(find find_by_sql calculate create create! update_all destroy destroy_all delete delete_all)
- INSTANCE_METHODS =
%w(update save save! destroy)
Class Method Summary collapse
Class Method Details
.all_methods ⇒ Object
58 59 60 |
# File 'lib/fiveruns/dash/recipes/activerecord.rb', line 58 def self.all_methods CLASS_METHODS.map { |m| "ActiveRecord::Base.#{m}" } + INSTANCE_METHODS.map { |m| "ActiveRecord::Base##{m}"} end |
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fiveruns/dash/recipes/activerecord.rb', line 5 def self.included(base) class << base CLASS_METHODS.each do |meth| head = meth tail = '' head, tail = meth[0..(meth.length-2)], meth[-1..-1] if %w(? !).include? meth[-1..-1] self.class_eval <<-EOM def #{head}_with_dash_context#{tail}(*args, &block) Fiveruns::Dash::ActiveRecordContext.with_model_context(self.name) do #{head}_without_dash_context#{tail}(*args, &block) end end EOM alias_method_chain(meth.to_sym, :dash_context) end end INSTANCE_METHODS.each do |meth| head = meth tail = '' head, tail = meth[0..meth.length-2], meth[-1..-1] if %w(? !).include? meth[-1..-1] base.class_eval <<-EOM def #{head}_with_dash_context#{tail}(*args, &block) Fiveruns::Dash::ActiveRecordContext.with_model_context(self.class.name) do #{head}_without_dash_context#{tail}(*args, &block) end end EOM base.alias_method_chain(meth.to_sym, :dash_context) end end |
.with_model_context(model_name) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fiveruns/dash/recipes/activerecord.rb', line 37 def self.with_model_context(model_name) ctx = Fiveruns::Dash::Context.context # don't change context if model context has already been set. return yield if ctx.size > 0 && ctx[-2] == 'model' && ctx[-1] == model_name original_context = Fiveruns::Dash::Context.context.dup begin if ctx[-2] == 'model' # Some models will internally load other models. Fiveruns::Dash::Context.context.pop Fiveruns::Dash::Context.context << model_name else Fiveruns::Dash::Context.context << 'model' Fiveruns::Dash::Context.context << model_name end return yield ensure Fiveruns::Dash::Context.set original_context end end |