Module: Motor::Configs::BuildUiAppTag
- Defined in:
- lib/motor/configs/build_ui_app_tag.rb
Constant Summary collapse
- CACHE_STORE =
if Motor.development? ActiveSupport::Cache::NullStore.new else ActiveSupport::Cache::MemoryStore.new(size: 5.megabytes) end
Class Method Summary collapse
- .add_default_links(links) ⇒ Object
- .alerts_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
- .app_tag_cache_key(cache_keys, current_user, current_ability) ⇒ Object
- .build_cache_key(cache_keys, key, current_user, current_ability) ⇒ String
-
.build_data(cache_keys = {}, current_user = nil, current_ability = nil) ⇒ Hash
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
- .call(current_user = nil, current_ability = nil, cache_keys: LoadFromCache.load_cache_keys) ⇒ Object
- .dashboards_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
- .database_names ⇒ Object
- .filter_links_for_user(current_user, links) ⇒ Object
- .forms_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
- .header_links_data_hash(current_user, current_ability, cache_key = nil) ⇒ Object
- .homepage_layout_data_hash(cache_key = nil) ⇒ Object
-
.i18n_data ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength.
- .queries_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
- .serialize_current_user(user) ⇒ Object
Class Method Details
.add_default_links(links) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 98 def add_default_links(links) new_links = links.clone unless links.find { |l| l['link_type'] == 'forms' } new_links.unshift({ 'name' => I18n.t('motor.forms'), 'link_type' => 'forms' }) end unless links.find { |l| l['link_type'] == 'reports' } new_links.unshift({ 'name' => I18n.t('motor.reports'), 'link_type' => 'reports' }) end new_links end |
.alerts_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
164 165 166 167 168 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 164 def alerts_data_hash(cache_key = nil, current_ability = nil) Motor::Configs::LoadFromCache.load_alerts(cache_key: cache_key, current_ability: current_ability) .as_json(only: %i[id name is_enabled updated_at], include: { tags: { only: %i[id name] } }) end |
.app_tag_cache_key(cache_keys, current_user, current_ability) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 24 def app_tag_cache_key(cache_keys, current_user, current_ability) key = "#{I18n.locale}#{cache_keys.hash}#{current_user&.id}#{current_ability&.rules_hash}" key += Motor::DefineArModels.defined_models_schema_md5.to_s if defined?(Motor::DefineArModels) if Rails.env.development? key += Digest::MD5.hexdigest(ActiveRecord::Base.descendants.map(&:object_id).sort.join) end key end |
.build_cache_key(cache_keys, key, current_user, current_ability) ⇒ String
82 83 84 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 82 def build_cache_key(cache_keys, key, current_user, current_ability) "#{cache_keys[key].hash}#{current_user&.id}#{current_ability&.rules_hash}" end |
.build_data(cache_keys = {}, current_user = nil, current_ability = nil) ⇒ Hash
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 39 def build_data(cache_keys = {}, current_user = nil, current_ability = nil) configs_cache_key = cache_keys[:configs] { version: Motor::VERSION, current_user: serialize_current_user(current_user), current_rules: current_ability.serialized_rules, audits_count: Motor::Audit.count, i18n: i18n_data, base_path: Motor::Admin.routes.url_helpers.motor_path, cable_path: Motor::Admin.routes.url_helpers.try(:motor_cable_path), admin_settings_path: Rails.application.routes.url_helpers.try(:admin_settings_general_path), active_storage_direct_uploads_enabled: ENV['MOTOR_ACTIVE_STORAGE_DIRECT_UPLOADS_ENABLED'].present?, schema: Motor::BuildSchema.call(cache_keys, current_ability), header_links: header_links_data_hash(current_user, current_ability, configs_cache_key), homepage_layout: homepage_layout_data_hash(configs_cache_key), databases: database_names, queries: queries_data_hash(build_cache_key(cache_keys, :queries, current_user, current_ability), current_ability), dashboards: dashboards_data_hash(build_cache_key(cache_keys, :dashboards, current_user, current_ability), current_ability), alerts: alerts_data_hash(build_cache_key(cache_keys, :alerts, current_user, current_ability), current_ability), forms: forms_data_hash(build_cache_key(cache_keys, :forms, current_user, current_ability), current_ability) } end |
.call(current_user = nil, current_ability = nil, cache_keys: LoadFromCache.load_cache_keys) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 15 def call(current_user = nil, current_ability = nil, cache_keys: LoadFromCache.load_cache_keys) CACHE_STORE.fetch(app_tag_cache_key(cache_keys, current_user, current_ability)) do CACHE_STORE.clear data = build_data(cache_keys, current_user, current_ability) Motor::ApplicationController.helpers.tag.div('', id: 'app', data: data) end end |
.dashboards_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
158 159 160 161 162 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 158 def dashboards_data_hash(cache_key = nil, current_ability = nil) Motor::Configs::LoadFromCache.load_dashboards(cache_key: cache_key, current_ability: current_ability) .as_json(only: %i[id title updated_at], include: { tags: { only: %i[id name] } }) end |
.database_names ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 140 def database_names if defined?(Motor::EncryptedConfig) Motor::DatabaseClasses.constants.map { |e| e.to_s.titleize } elsif ActiveRecord::Base.configurations.try(:configurations) ActiveRecord::Base.configurations.configurations .select { |c| c.env_name == Rails.env } .map { |c| c.try(:name) || c.try(:spec_name) }.compact else ['primary'] end end |
.filter_links_for_user(current_user, links) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 112 def filter_links_for_user(current_user, links) links.select do |link| conditions = link['conditions'] next true if conditions.blank? conditions.all? do |cond| field_name = if cond['field'] == 'role' && !current_user.respond_to?(:role) :role_names else cond['field'].to_sym end next false unless field_name.in?(%i[email role role_names]) next false unless current_user.respond_to?(field_name) Array.wrap(current_user.public_send(field_name)).intersection(Array.wrap(cond['value'])).present? end end end |
.forms_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
170 171 172 173 174 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 170 def forms_data_hash(cache_key = nil, current_ability = nil) Motor::Configs::LoadFromCache.load_forms(cache_key: cache_key, current_ability: current_ability) .as_json(only: %i[id name updated_at], include: { tags: { only: %i[id name] } }) end |
.header_links_data_hash(current_user, current_ability, cache_key = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 86 def header_links_data_hash(current_user, current_ability, cache_key = nil) configs = Motor::Configs::LoadFromCache.load_configs(cache_key: cache_key) links = configs.find { |c| c.key == 'header.links' }&.value || [] links = add_default_links(links) return links unless current_user return links if current_ability.can?(:manage, :all) filter_links_for_user(current_user, links) end |
.homepage_layout_data_hash(cache_key = nil) ⇒ Object
134 135 136 137 138 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 134 def homepage_layout_data_hash(cache_key = nil) configs = Motor::Configs::LoadFromCache.load_configs(cache_key: cache_key) configs.find { |c| c.key == 'homepage.layout' }&.value || [] end |
.i18n_data ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength
66 67 68 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 66 def i18n_data I18n.t('motor', default: I18n.t('motor', locale: :en)) end |
.queries_data_hash(cache_key = nil, current_ability = nil) ⇒ Object
152 153 154 155 156 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 152 def queries_data_hash(cache_key = nil, current_ability = nil) Motor::Configs::LoadFromCache.load_queries(cache_key: cache_key, current_ability: current_ability) .as_json(only: %i[id name updated_at], include: { tags: { only: %i[id name] } }) end |
.serialize_current_user(user) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/motor/configs/build_ui_app_tag.rb', line 70 def serialize_current_user(user) return unless user attrs = user.as_json(only: %i[id email first_name last_name]) attrs['role'] = user.role if user.respond_to?(:role) attrs['role_names'] = user.role_names if user.respond_to?(:role_names) attrs end |