Module: RKit::Decorator::ActiveRecordExtend
- Extended by:
- ActiveRecordExtend
- Included in:
- ActiveRecordExtend
- Defined in:
- lib/r_kit/decorator/active_record_extend.rb
Instance Attribute Summary collapse
-
#decorator_klass ⇒ Object
Returns the value of attribute decorator_klass.
Instance Method Summary collapse
- #acts_as_decorables(base = nil, &block) ⇒ Object
- #decorate(view_context: nil) ⇒ Object
- #decorator_klass_from(arg) ⇒ Object
- #decorator_klass_from_class(base) ⇒ Object
- #decorator_klass_from_module(mod) ⇒ Object
- #decorator_klass_from_nil_class(*args) ⇒ Object
- #decorator_klass_from_proc(block) ⇒ Object
-
#define_decorator(arg) ⇒ Object
TODO: all the methods below this comment should be private, even more, they should be in a “decorator_finder_creator_definer”, and not included in active_record.
- #define_instance_methods ⇒ Object
Instance Attribute Details
#decorator_klass ⇒ Object
Returns the value of attribute decorator_klass.
2 3 4 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 2 def decorator_klass @decorator_klass end |
Instance Method Details
#acts_as_decorables(base = nil, &block) ⇒ Object
4 5 6 7 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 4 def acts_as_decorables base = nil, &block define_decorator base || block define_instance_methods end |
#decorate(view_context: nil) ⇒ Object
51 52 53 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 51 def decorate view_context: nil all.map{ |record| record.decorate view_context: view_context } end |
#decorator_klass_from(arg) ⇒ Object
15 16 17 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 15 def decorator_klass_from arg send "decorator_klass_from_#{ arg.class.name.underscore }", arg end |
#decorator_klass_from_class(base) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 23 def decorator_klass_from_class base if base <=> RKit::Decorator::Base base else base.tap do |base| base.send :include, Module.new{ include refine(RKit::Decorator::Base){} } base.extend Module.new{ include refine(RKit::Decorator::Base.singleton_class){} } RKit::Decorator::Base.inherited base end end end |
#decorator_klass_from_module(mod) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 36 def decorator_klass_from_module mod namespace = (mod.name.deconstantize.presence || 'Object').constantize const_name = mod.name.demodulize namespace.send :remove_const, const_name namespace.const_set const_name, Class.new(RKit::Decorator::Base){ include mod } end |
#decorator_klass_from_nil_class(*args) ⇒ Object
19 20 21 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 19 def decorator_klass_from_nil_class *args decorator_klass_from "#{ name }Decorator".constantize end |
#decorator_klass_from_proc(block) ⇒ Object
44 45 46 47 48 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 44 def decorator_klass_from_proc block (name.deconstantize.presence || 'Object') .constantize .const_set "#{ name.demodulize }Decorator", Class.new(RKit::Decorator::Base, &block) end |
#define_decorator(arg) ⇒ Object
TODO: all the methods below this comment should be private, even more, they should be in a “decorator_finder_creator_definer”, and not included in active_record. SRP guys !
10 11 12 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 10 def define_decorator arg @decorator_klass = decorator_klass_from arg end |
#define_instance_methods ⇒ Object
56 57 58 59 60 |
# File 'lib/r_kit/decorator/active_record_extend.rb', line 56 def define_instance_methods define_method 'decorate' do |view_context: nil| self.class.decorator_klass.new self, view_context: view_context end end |