Module: ConsciousConcern
- Includes:
- ActiveSupport::Concern
- Defined in:
- lib/conscious_concern.rb,
lib/conscious_concern/eager_loader.rb
Overview
Decorates ActiveSupport::Concern
Defined Under Namespace
Classes: EagerLoader
Class Method Summary collapse
- .extended(base) ⇒ Object
- .extenders ⇒ Object
- .load_classes(*custom_dirs, engine: nil, rails: true) ⇒ Object
Instance Method Summary collapse
-
#classes ⇒ Object
(also: #_classes)
underscore aliases protect core functionality against overrides in extenders.
- #classes_loaded_callbacks ⇒ Object (also: #_classes_loaded_callbacks)
- #controllers ⇒ Object (also: #_controllers)
- #included(base = nil, &block) ⇒ Object
- #let_controllers(method_args_hash) ⇒ Object
- #models ⇒ Object (also: #_models)
- #prepended(base = nil) ⇒ Object
- #resources(options = { only: [] }, &resource_routing_block) ⇒ Object
- #route_keys ⇒ Object (also: #_route_keys)
- #tables ⇒ Object (also: #_tables)
Class Method Details
.extended(base) ⇒ Object
23 24 25 26 |
# File 'lib/conscious_concern.rb', line 23 def self.extended(base) extenders << base ActiveSupport::Concern.extended(base) end |
.extenders ⇒ Object
19 20 21 |
# File 'lib/conscious_concern.rb', line 19 def self.extenders @_extenders ||= Set.new end |
.load_classes(*custom_dirs, engine: nil, rails: true) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/conscious_concern.rb', line 10 def self.load_classes(*custom_dirs, engine: nil, rails: true) std_dirs = %w(controllers models) EagerLoader.load_classes_in_engine_dirs(engine, *std_dirs) if engine EagerLoader.load_classes_in_rails_dirs(*std_dirs) if rails custom_dirs.each { |dir| EagerLoader.load_classes_in_dir(dir) } extenders.each { |concern| concern._classes_loaded_callbacks.each(&:call) } end |
Instance Method Details
#classes ⇒ Object Also known as: _classes
underscore aliases protect core functionality against overrides in extenders
40 41 42 |
# File 'lib/conscious_concern.rb', line 40 def classes @_classes ||= Set.new end |
#classes_loaded_callbacks ⇒ Object Also known as: _classes_loaded_callbacks
83 84 85 |
# File 'lib/conscious_concern.rb', line 83 def classes_loaded_callbacks @_classes_loaded_callbacks ||= [] end |
#controllers ⇒ Object Also known as: _controllers
45 46 47 48 |
# File 'lib/conscious_concern.rb', line 45 def controllers return _classes unless _model_concern? _classes.map { |m| _try_constant("#{m.to_s.pluralize}Controller") }.compact end |
#included(base = nil, &block) ⇒ Object
28 29 30 31 |
# File 'lib/conscious_concern.rb', line 28 def included(base = nil, &block) _classes << base if base && base.is_a?(Class) super unless base.nil? && instance_variable_defined?(:@_included_block) end |
#let_controllers(method_args_hash) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/conscious_concern.rb', line 75 def let_controllers(method_args_hash) _classes_loaded_callbacks << proc do method_args_hash.each do |method, args| _controllers.each { |ctr| ctr.send(method, *args) } end end end |
#models ⇒ Object Also known as: _models
51 52 53 54 |
# File 'lib/conscious_concern.rb', line 51 def models return _classes if _model_concern? _classes.map { |ctr| _try_constant(ctr.controller_name) }.compact end |
#prepended(base = nil) ⇒ Object
33 34 35 36 |
# File 'lib/conscious_concern.rb', line 33 def prepended(base = nil) _classes << base if base && base.is_a?(Class) super end |
#resources(options = { only: [] }, &resource_routing_block) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/conscious_concern.rb', line 57 def resources( = { only: [] }, &resource_routing_block) resource_args = _route_keys app_routes_block = resource_routing_block.binding.eval('self') app_routes_block.instance_eval do resources(*resource_args, , &resource_routing_block) end end |
#route_keys ⇒ Object Also known as: _route_keys
65 66 67 |
# File 'lib/conscious_concern.rb', line 65 def route_keys _models.map { |model| model.try(:model_name).try(:route_key) }.compact end |
#tables ⇒ Object Also known as: _tables
70 71 72 |
# File 'lib/conscious_concern.rb', line 70 def tables _models.map { |model| model.try(:table_name) }.compact end |