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

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.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)


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.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Raises:

  • (ArgumentError)

    if model repository is not registered for the given model



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.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


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.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)


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.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Raises:

  • (ArgumentError)

    if model repository is not registered for the given model



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.

Parameters:

  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


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.

Parameters:

  • model_class (Class)
  • repos (Array<String, Esse::Index, Esse::Repo>)

Returns:

  • (Boolean)


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

Raises:

  • (ArgumentError)


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_namesObject



18
19
20
# File 'lib/esse/active_record/hooks.rb', line 18

def model_names
  models.map(&:to_s)
end

.modelsObject



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