Module: Tramway

Extended by:
Forms, Navbar, Notifications, RecordsModels, SingletonModels, WelcomePageActions
Defined in:
lib/tramway.rb,
lib/tramway/version.rb,
lib/tramway/generators.rb,
app/helpers/tramway/focus_generator_helper.rb,
app/controllers/tramway/singletons_controller.rb

Defined Under Namespace

Modules: ActionsHelper, AdditionalButtonsBuilder, ApplicationHelper, AuthManagement, CasesHelper, ClassNameHelpers, Collections, CopyToClipboardHelper, Filtering, FocusGeneratorHelper, Forms, FrontendHelper, Generators, InputsHelper, ModelHelper, Navbar, NavbarHelper, Notifications, RecordRoutesHelper, RecordsHelper, RecordsModels, RussianCasesHelper, SingletonHelper, SingletonModels, StateMachineButtonsHelper, TitleHelper, WelcomePageActions Classes: Application, ApplicationController, ApplicationDecoratedCollection, ApplicationDecorator, ApplicationForm, ApplicationRecord, Collection, Engine, Error, ExtendableForm, ExtendedApplicationForm, FormCreator, HasAndBelongsToManyRecordsController, RecordsController, SessionForm, SessionsController, SingletonsController, User, UserDecorator, WelcomeController

Constant Summary collapse

VERSION =
'0.1.1.1'

Class Attribute Summary collapse

Attributes included from WelcomePageActions

#welcome_page_actions

Attributes included from Forms

#forms

Class Method Summary collapse

Methods included from Navbar

navbar_items_for, navbar_structure

Methods included from Notifications

notificable_queries, set_notificable_queries

Methods included from SingletonModels

set_singleton_models, singleton_models, singleton_models_for

Methods included from RecordsModels

available_models, available_models_for, clear_available_models!, set_available_models

Class Attribute Details

.applicationObject (readonly)

Returns the value of attribute application.



35
36
37
# File 'lib/tramway.rb', line 35

def application
  @application
end

.customized_admin_navbarObject (readonly)

Returns the value of attribute customized_admin_navbar.



35
36
37
# File 'lib/tramway.rb', line 35

def customized_admin_navbar
  @customized_admin_navbar
end

Class Method Details

.action_is_available?(record, project:, role:, model_name:, action:) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/tramway.rb', line 70

def action_is_available?(record, project:, role:, model_name:, action:)
  project = project.underscore.to_sym unless project.is_a? Symbol
  actions = select_actions(project: project, role: role.to_sym, model_name: model_name)
  availability = compute_availability actions, action

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

  availability.call record
end

.admin_modelObject



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

def admin_model
  auth_config.first[:user_model]
end

.application_objectObject



27
28
29
# File 'lib/tramway.rb', line 27

def application_object
  @application&.model_class.present? ? @application.model_class.first : @application
end

.auth_attributesObject



124
125
126
127
128
129
# File 'lib/tramway.rb', line 124

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

.auth_configObject



103
104
105
106
# File 'lib/tramway.rb', line 103

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

.auth_config=(params) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/tramway.rb', line 108

def auth_config=(params)
  case params
  when Hash
    @@auth_config = [params]
  when Array
    @@auth_config = params
  end
end

.compute_availability(actions, action) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/tramway.rb', line 81

def compute_availability(actions, action)
  case actions
  when Array
    actions.select { |a| a.to_sym == action.to_sym }.first
  when Hash
    actions[action.to_sym]
  end
end

.engine_class(project) ⇒ Object



44
45
46
47
# File 'lib/tramway.rb', line 44

def engine_class(project)
  class_name = "Tramway::#{project.to_s.camelize}"
  class_name.classify.safe_constantize
end

.get_models_by_key(checked_models, project, role) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tramway.rb', line 53

def get_models_by_key(checked_models, project, role)
  unless project.present?
    Tramway::Error.raise_error(
      :tramway,
      :project,
      :no_instance_of_application_class,
      application_class: Tramway.application.model_class
    )
  end

  checked_models && checked_models[project]&.dig(role)&.keys || []
end

.initialize_application(**options) ⇒ Object



21
22
23
24
25
# File 'lib/tramway.rb', line 21

def initialize_application(**options)
  @application ||= Tramway::Application.new

  options.each { |attr, value| @application.send "#{attr}=", value }
end

.models_array(models_type:, role:) ⇒ Object



66
67
68
# File 'lib/tramway.rb', line 66

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

.project_is_engine?(project) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tramway.rb', line 49

def project_is_engine?(project)
  engine_class(project)
end

.rootObject



31
32
33
# File 'lib/tramway.rb', line 31

def root
  File.dirname __dir__
end

.root_path_for(user_class) ⇒ Object



136
137
138
# File 'lib/tramway.rb', line 136

def root_path_for(user_class)
  @root_path&.dig(user_class) || '/'
end

.root_path_for=(**options) ⇒ Object



131
132
133
134
# File 'lib/tramway.rb', line 131

def root_path_for=(**options)
  @root_path ||= {}
  @root_path.merge! options
end

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



90
91
92
93
# File 'lib/tramway.rb', line 90

def select_actions(project:, role:, model_name:)
  stringify = ->(models) { stringify_keys(models&.dig(project, role))&.dig(model_name) }
  stringify.call(@singleton_models) || stringify.call(@available_models)
end

.stringify_keys(hash) ⇒ Object



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

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

.user_based_modelsObject



117
118
119
120
121
122
# File 'lib/tramway.rb', line 117

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