Module: Tramway::Api

Extended by:
RecordsModels, SingletonModels
Defined in:
lib/tramway/api.rb,
lib/tramway/api/engine.rb,
lib/tramway/api/version.rb,
app/jobs/tramway/api/application_job.rb,
app/models/tramway/api/application_record.rb,
app/helpers/tramway/api/application_helper.rb,
app/mailers/tramway/api/application_mailer.rb,
app/controllers/tramway/api/application_controller.rb,
app/controllers/tramway/api/v1/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper, RecordsModels, SingletonModels, V1 Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, AuthenticationForm, Engine

Constant Summary collapse

VERSION =
'2.0.0.2'

Class Method Summary collapse

Methods included from SingletonModels

set_singleton_models, singleton_models, singleton_models_for

Methods included from RecordsModels

available_models, available_models_for, set_available_models

Class Method Details

.action_is_available(project:, role: :open, model_name:, action:, current_user: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tramway/api.rb', line 57

def action_is_available(project:, role: :open, model_name:, action:, current_user: nil)
  actions = select_actions(project: project, role: role, model_name: model_name)
  if actions.present? && !actions.is_a?(Array)
    raise "Looks like you did not used array type to define action permissions. Remember it should be this way: `#{model_name} => [ :#{action} ]` or `#{model_name} => [ { #{action}: lambda { |record, current_user| your_condition } } ]`"
  end

  availability = actions&.select do |a|
    if a.is_a? Symbol
      a == action.to_sym
    elsif a.is_a? Hash
      a.keys.first.to_sym == action.to_sym
    end
  end&.first

  return false unless availability.present?
  return true if availability.is_a? Symbol

  availability.values.first
end

.auth_attributesObject



32
33
34
35
36
37
# File 'lib/tramway/api.rb', line 32

def auth_attributes
  @@auth_config ||= []
  @@auth_config.reduce({}) do |hash, conf|
    hash.merge! conf[:user_model] => conf[:auth_attributes]
  end
end

.auth_configObject



13
14
15
# File 'lib/tramway/api.rb', line 13

def auth_config
  @@auth_config ||= [{ user_model: ::Tramway::User::User, auth_attributes: :email }]
end

.auth_config=(params) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/tramway/api.rb', line 17

def auth_config=(params)
  if params.is_a? Hash
    @@auth_config = [params]
  elsif params.is_a? Array
    @@auth_config = params
  end
end

.default_id_method_of(model:) ⇒ Object



98
99
100
101
# File 'lib/tramway/api.rb', line 98

def default_id_method_of(model:)
  @@id_methods ||= {}
  @@id_methods.dig(model.to_s, :default)
end

.get_models_by_key(checked_models, project, role) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tramway/api.rb', line 39

def get_models_by_key(checked_models, project, role)
  unless project.present?
    error = Tramway::Error.new(
      plugin: :admin,
      method: :get_models_by_key,
      message: "Looks like you have not create at lease one instance of #{Tramway::Core.application.model_class} model"
    )
    raise error.message
  end
  checked_models && checked_models != [] && checked_models[project][role]&.keys || []
end

.id_methods_of(options = {}) ⇒ Object



87
88
89
90
91
92
# File 'lib/tramway/api.rb', line 87

def id_methods_of(options = {})
  @@id_methods ||= {}
  @@id_methods.merge!(options.reduce({}) do |hash, pair|
    hash.merge! pair[0].to_s => pair[1]
  end)
end

.models_array(models_type:, role:) ⇒ Object



51
52
53
54
55
# File 'lib/tramway/api.rb', line 51

def models_array(models_type:, role:)
  instance_variable_get("@#{models_type}_models")&.map do |projects|
    projects.last[role]&.keys
  end&.flatten || []
end

.other_id_methods_of(model:) ⇒ Object



94
95
96
# File 'lib/tramway/api.rb', line 94

def other_id_methods_of(model:)
  @@id_methods[model.to_s][:other]
end

.select_actions(project:, role:, model_name:) ⇒ Object



77
78
79
# File 'lib/tramway/api.rb', line 77

def select_actions(project:, role:, model_name:)
  stringify_keys(@singleton_models&.dig(project, role))&.dig(model_name) || stringify_keys(@available_models&.dig(project, role))&.dig(model_name)
end

.stringify_keys(hash) ⇒ Object



81
82
83
84
85
# File 'lib/tramway/api.rb', line 81

def stringify_keys(hash)
  hash&.reduce({}) do |new_hash, pair|
    new_hash.merge! pair[0].to_s => pair[1]
  end
end

.user_based_modelsObject



25
26
27
28
29
30
# File 'lib/tramway/api.rb', line 25

def user_based_models
  @@auth_config ||= []
  @@auth_config.map do |conf|
    conf[:user_model]
  end
end