Module: Esse::ActiveRecord::Hooks
- Defined in:
- lib/esse/active_record/hooks.rb
Constant Summary collapse
- STORE_STATE_KEY =
:esse_active_record_hooks
Class Method Summary collapse
-
.disable!(*repos) ⇒ void
Global disable indexing callbacks.
-
.disable_model!(model_class, *repos) ⇒ void
Disable model indexing callbacks for the given model.
-
.disabled?(*repos) ⇒ Boolean
Check if the given repository is enabled for indexing.
-
.enable!(*repos) ⇒ void
Global enable indexing callbacks.
-
.enable_model!(model_class, *repos) ⇒ void
Enable model indexing callbacks for the given model.
-
.enabled?(*repos) ⇒ Boolean
Check if the given repository is enabled for indexing.
-
.enabled_for_model?(model_class, *repos) ⇒ Boolean
Check if the given model is enabled for indexing.
- .ensure_registered_model_class!(model_class) ⇒ Object
- .model_names ⇒ Object
- .models ⇒ Object
- .register_model(model_class) ⇒ Object
- .resolve_index_repository(name) ⇒ Object
-
.without_indexing(*repos) ⇒ Object
Disable indexing callbacks execution for the block execution.
-
.without_indexing_for_model(model_class, *repos) ⇒ Object
Disable model indexing callbacks execution for the block execution for the given model.
Class Method Details
.disable!(*repos) ⇒ void
This method returns an undefined value.
Global disable indexing callbacks. If no repository is specified, all repositories will be disabled.
34 35 36 37 38 |
# File 'lib/esse/active_record/hooks.rb', line 34 def disable!(*repos) filter_repositories(*repos).each do |repo| state[:repos][repo] = false end end |
.disable_model!(model_class, *repos) ⇒ void
This method returns an undefined value.
Disable model indexing callbacks for the given model. If no repository is specified, all repositories will be disabled.
75 76 77 78 79 80 |
# File 'lib/esse/active_record/hooks.rb', line 75 def disable_model!(model_class, *repos) ensure_registered_model_class!(model_class) filter_model_repositories(model_class, *repos).each do |repo| state[:models][model_class][repo] = false end end |
.disabled?(*repos) ⇒ Boolean
Check if the given repository is enabled for indexing. If no repository is specified, all repositories will be checked.
44 45 46 |
# File 'lib/esse/active_record/hooks.rb', line 44 def disabled?(*repos) filter_repositories(*repos).all? { |repo| !state[:repos][repo] } end |
.enable!(*repos) ⇒ void
This method returns an undefined value.
Global enable indexing callbacks. If no repository is specified, all repositories will be enabled.
25 26 27 28 29 |
# File 'lib/esse/active_record/hooks.rb', line 25 def enable!(*repos) filter_repositories(*repos).each do |repo| state[:repos][repo] = true end end |
.enable_model!(model_class, *repos) ⇒ void
This method returns an undefined value.
Enable model indexing callbacks for the given model. If no repository is specified, all repositories will be enabled.
62 63 64 65 66 67 |
# File 'lib/esse/active_record/hooks.rb', line 62 def enable_model!(model_class, *repos) ensure_registered_model_class!(model_class) filter_model_repositories(model_class, *repos).each do |repo| state[:models][model_class][repo] = true end end |
.enabled?(*repos) ⇒ Boolean
Check if the given repository is enabled for indexing. If no repository is specified, all repositories will be checked.
52 53 54 |
# File 'lib/esse/active_record/hooks.rb', line 52 def enabled?(*repos) filter_repositories(*repos).all? { |repo| state[:repos][repo] } end |
.enabled_for_model?(model_class, *repos) ⇒ Boolean
Check if the given model is enabled for indexing. If no repository is specified, all repositories will be checked.
93 94 95 96 97 98 99 |
# File 'lib/esse/active_record/hooks.rb', line 93 def enabled_for_model?(model_class, *repos) return false unless registered_model_class?(model_class) filter_model_repositories(model_class, *repos).all? do |repo| state.dig(:models, model_class, repo) != false end end |
.ensure_registered_model_class!(model_class) ⇒ Object
82 83 84 85 86 |
# File 'lib/esse/active_record/hooks.rb', line 82 def ensure_registered_model_class!(model_class) return if registered_model_class?(model_class) raise ArgumentError, "Model class #{model_class} is not registered. The model should inherit from Esse::ActiveRecord::Model and have a `index_callback' callback defined" end |
.model_names ⇒ Object
18 19 20 |
# File 'lib/esse/active_record/hooks.rb', line 18 def model_names models.map(&:to_s) end |
.models ⇒ Object
14 15 16 |
# File 'lib/esse/active_record/hooks.rb', line 14 def models @models || [] end |
.register_model(model_class) ⇒ Object
9 10 11 12 |
# File 'lib/esse/active_record/hooks.rb', line 9 def register_model(model_class) @models ||= [] @models |= [model_class] end |
.resolve_index_repository(name) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/esse/active_record/hooks.rb', line 130 def resolve_index_repository(name) index_name, repo_name = name.to_s.underscore.split('::').join('/').split(':', 2) if index_name !~ /(I|_i)ndex$/ && index_name !~ /_index\/([\w_]+)$/ index_name = format('%<index_name>s_index', index_name: index_name) end klass = index_name.classify.constantize return klass if klass <= Esse::Repository repo_name.present? ? klass.repo(repo_name) : klass.repo end |
.without_indexing(*repos) ⇒ Object
Disable indexing callbacks execution for the block execution. Example:
Esse::ActiveRecord::Hooks.without_indexing { User.create! }
Esse::ActiveRecord::Hooks.without_indexing(UsersIndex, AccountsIndex.repo(:user)) { User.create! }
105 106 107 108 109 110 111 112 |
# File 'lib/esse/active_record/hooks.rb', line 105 def without_indexing(*repos) state_before_disable = state[:repos].dup disable!(*repos) yield ensure state[:repos] = state_before_disable end |
.without_indexing_for_model(model_class, *repos) ⇒ Object
Disable model indexing callbacks execution for the block execution for the given model. Example:
BroadcastChanges.without_indexing_for_model(User) { }
BroadcastChanges.without_indexing_for_model(User, :datasync, :other) { }
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/esse/active_record/hooks.rb', line 118 def without_indexing_for_model(model_class, *repos) state_before_disable = state[:models].dig(model_class).dup disable_model!(model_class, *repos) yield ensure if state_before_disable.nil? state[:models].delete(model_class) else state[:models][model_class] = state_before_disable end end |