Module: BarkestCore

Defined in:
lib/barkest_core.rb,
lib/barkest_core/engine.rb,
lib/barkest_core/version.rb,
app/models/barkest_core/db_table.rb,
app/models/barkest_core/log_entry.rb,
app/models/barkest_core/work_path.rb,
app/models/barkest_core/user_alert.rb,
app/helpers/barkest_core/pdf_helper.rb,
app/models/barkest_core/auth_config.rb,
app/helpers/barkest_core/form_helper.rb,
app/helpers/barkest_core/html_helper.rb,
app/helpers/barkest_core/misc_helper.rb,
app/mailers/barkest_core/user_mailer.rb,
app/models/barkest_core/email_config.rb,
app/models/barkest_core/user_manager.rb,
app/helpers/barkest_core/users_helper.rb,
app/mailers/barkest_core/contact_form.rb,
app/models/barkest_core/global_status.rb,
app/models/barkest_core/not_logged_in.rb,
lib/barkest_core/concerns/date_parser.rb,
lib/barkest_core/concerns/named_model.rb,
lib/barkest_core/handlers/csv_handler.rb,
app/helpers/barkest_core/status_helper.rb,
lib/barkest_core/concerns/email_tester.rb,
app/models/barkest_core/contact_message.rb,
app/models/barkest_core/database_config.rb,
app/models/barkest_core/ms_sql_function.rb,
lib/barkest_core/concerns/number_parser.rb,
app/helpers/barkest_core/sessions_helper.rb,
app/models/barkest_core/log_view_options.rb,
lib/barkest_core/concerns/boolean_parser.rb,
lib/barkest_core/concerns/utc_conversion.rb,
app/helpers/barkest_core/recaptcha_helper.rb,
app/models/barkest_core/authorize_failure.rb,
app/models/barkest_core/ms_sql_definition.rb,
app/models/barkest_core/pdf_table_builder.rb,
app/models/barkest_core/self_update_config.rb,
lib/barkest_core/concerns/encrypted_fields.rb,
app/helpers/barkest_core/application_helper.rb,
app/models/barkest_core/ms_sql_db_definition.rb,
app/models/barkest_core/user_alert_generators.rb,
lib/generators/barkest_core/install_generator.rb,
app/controllers/barkest_core/testsub_controller.rb,
lib/barkest_core/extensions/main_app_extensions.rb,
app/mailers/barkest_core/application_mailer_base.rb,
app/controllers/barkest_core/engine_controller_base.rb,
lib/barkest_core/concerns/association_with_defaults.rb,
lib/barkest_core/extensions/active_record_extensions.rb,
app/controllers/barkest_core/application_controller_base.rb

Defined Under Namespace

Modules: ApplicationHelper, AssociationWithDefaults, BooleanParser, ConnectionAdapterExtensions, CsvHandler, DateParser, EmailTester, EncryptedFields, FormHelper, HtmlHelper, MainAppUrlHelper, MiscHelper, NamedModel, NumberParser, PdfHelper, RecaptchaHelper, SessionsHelper, StatusHelper, UsersHelper, UtcConversion Classes: ApplicationControllerBase, ApplicationMailerBase, AuthConfig, AuthorizeFailure, ContactForm, ContactMessage, DatabaseConfig, DbTable, EmailConfig, Engine, EngineControllerBase, GlobalStatus, InstallGenerator, LogEntry, LogViewOptions, MsSqlDbDefinition, MsSqlDefinition, MsSqlFunction, NotLoggedIn, PdfTableBuilder, SelfUpdateConfig, TestsubController, UserAlert, UserAlertGenerators, UserMailer, UserManager, WorkPath

Constant Summary collapse

VERSION =
'1.5.4.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_key_gem_pattern(pattern) ⇒ Object

Adds a key gem pattern to the default gem_list results.



223
224
225
226
227
228
229
# File 'lib/barkest_core.rb', line 223

def self.add_key_gem_pattern(pattern)
  pattern = pattern.to_s.downcase
  unless pattern.blank?
    key_gem_patterns << pattern unless key_gem_patterns.include?(pattern)
  end
  key_gem_patterns.include? pattern
end

.auth_configObject

Gets the authorization configuration.

Basically this does two things. 1) It will enable/disable internal database authentication. 2) It will enable/disable ldap authentication.

If ldap authentication is not configured then internal database authentication is forcibly enabled. Both modes can be active simultaneously.

Auth settings are stored under the :auth key within SystemConfig.

Returned keys will be symbols.



145
146
147
148
149
150
151
152
153
154
# File 'lib/barkest_core.rb', line 145

def self.auth_config
  @auth_config ||=
      begin
        cfg = auth_config_defaults(nil).symbolize_keys.merge( (SystemConfig.get(:auth) || {}).symbolize_keys )

        cfg[:enable_db_auth] = true unless cfg[:enable_ldap_auth]

        cfg
      end
end

.auth_config_defaults(new_defaults = {}) ⇒ Object

Provides the defaults to be provided by auth_config when settings are missing.



158
159
160
161
162
163
164
165
# File 'lib/barkest_core.rb', line 158

def self.auth_config_defaults(new_defaults = {})
  @auth_config_defaults = nil if new_defaults
  @auth_config_defaults ||=
      {
          enable_db_auth: true,
          enable_ldap_auth: false,
      }.merge((new_defaults || {}).symbolize_keys)
end

.configObject

:nodoc:



168
169
170
# File 'lib/barkest_core.rb', line 168

def self.config
  BarkestCore::Engine.config
end

.db_config(other = nil, env = nil, convert_extra = true) ⇒ Object

Gets the database configuration for the auth models.

The barkest_core database must be defined in database.yml. This will either be an explicit barkest_core section or a normal environment section (ie - production). However other databases can be defined via the SystemConfig model.

The database.yml file is checked first, followed by SystemConfig. If neither of those defines the specific database, then we fall back on database.yml to load the global configuration for the environment.

For instance, if you want the configuration for :mydb in the :development environment:

  • Check in database.yml for “mydb_development”.

  • Check in database.yml for “mydb”.

  • Check in SystemConfig for “mydb” (unless “mydb” == “barkest_core”).

  • Check in database.yml for “development”.

  • Fall back on default connection used by ActiveRecord::Base.

Returned hash will have symbol keys.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/barkest_core.rb', line 37

def self.db_config(other = nil, env = nil, convert_extra = true)
  if other
    @db_configs ||= {}

    other = other.to_sym
    env = (env || Rails.env).to_sym
    key = :"#{other}_#{env}"

    @db_configs[key] ||=
      begin
        avail = avail_db_configs

        # for any db connection other than the core connection, check
        # in the system config table as well.
        syscfg = (other == :barkest_core) ? nil : SystemConfig.get(other)
        defcfg = (other == :barkest_core) ? avail[env] : db_config_defaults(other)

        # Preference
        (
            avail[key] ||                         # 1: barkest_core_development
            avail[other] ||                       # 2: barkest_core
            syscfg ||                             # 3: SystemConfig: barkest_core
            defcfg ||                             # 4: YAML[env] or defaults depending on db name
            ActiveRecord::Base.connection_config  # 5: default connection (hopefully never gets used)
        ).merge(defcfg.select do |k,_|
          [ # reset name, type, and label for extra values.
              :extra_1_name, :extra_1_type, :extra_1_label,
              :extra_2_name, :extra_2_type, :extra_2_label,
              :extra_3_name, :extra_3_type, :extra_3_label,
              :extra_4_name, :extra_4_type, :extra_4_label,
              :extra_5_name, :extra_5_type, :extra_5_label
          ].include?(k)
        end)
      end
          .symbolize_keys

    if convert_extra
      @db_configs[:"#{key}_converted"] ||= BarkestCore::DatabaseConfig.new(@db_configs[key]).to_h(true)
    else
      @db_configs[key]
    end
  elsif env
    db_config(:barkest_core, env, convert_extra)
  else
    @db_config ||= db_config(:barkest_core, Rails.env, false)
  end
end

.db_config_is_file_based?(db_name) ⇒ Boolean

Determines if the configuration for the specified database is stored in the global configuration file.

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/barkest_core.rb', line 87

def self.db_config_is_file_based?(db_name)
  avail = avail_db_configs.keys
  avail.include?(:"#{db_name}_#{Rails.env}") || avail.include?(:"#{db_name}")
end

.email_configObject

Gets the email configuration for the application.

Email settings are stored under the :email key within SystemConfig.

Returned keys will be symbols.



115
116
117
# File 'lib/barkest_core.rb', line 115

def self.email_config
  @email_config ||= email_config_defaults(nil).merge( (SystemConfig.get(:email) || {}).symbolize_keys )
end

.email_config_defaults(new_defaults = {}) ⇒ Object

Provides the defaults to be returned by email_config when settings are missing.



121
122
123
124
125
126
127
128
129
130
# File 'lib/barkest_core.rb', line 121

def self.email_config_defaults(new_defaults = {})
  @email_config_defaults = nil if new_defaults
  @email_config_defaults ||=
      {
          config_mode: :none,
          default_sender: '[email protected]',
          default_recipient: '[email protected]',
          default_hostname: 'localhost',
      }.merge((new_defaults || {}).symbolize_keys)
end

.gem_list(*patterns) ⇒ Object

Gets a list of key gems with their versions.

This is useful for informational displays such as brief application version diplays.

Supply one or more patterns for gem names. If you supply none, then the default pattern list is used.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/barkest_core.rb', line 195

def self.gem_list(*patterns)
  ret = []

  if patterns.blank?
    patterns = key_gem_patterns
  elsif patterns.first.is_a?(TrueClass)
    patterns = key_gem_patterns + patterns[1..-1]
  elsif patterns.last.is_a?(TrueClass)
    patterns = patterns[0...-1] + key_gem_patterns
  end

  patterns = patterns.flatten.inject([]) { |memo,v| memo << v unless memo.include?(v); memo }

  Gem::Specification.to_a.sort{|a,b| a.name <=> b.name}.each do |gem|
    patterns.each do |pat|
      if pat.is_a?(String) && gem.name == pat
        ret << [ gem.name, gem.version.to_s ]
      elsif pat.is_a?(Regexp) && pat.match(gem.name)
        ret << [ gem.name, gem.version.to_s ]
      end
    end
  end

  ret
end

.gem_root_pathObject

Gets the root path of the BarkestCore gem.



281
282
283
# File 'lib/barkest_core.rb', line 281

def self.gem_root_path
  @gem_root_path ||= File.expand_path('../..', __FILE__)
end

.register_anon_menu(partial_path) ⇒ Object

Registers a partial view to build up the anonymous header menu.



233
234
235
236
237
# File 'lib/barkest_core.rb', line 233

def self.register_anon_menu(partial_path)
  unless partial_path.blank?
    anon_menu_registry << partial_path unless anon_menu_registry.include?(partial_path)
  end
end

.register_auth_menu(partial_path) ⇒ Object

Registers a partial view to build up the authenticated header menu.



241
242
243
244
245
# File 'lib/barkest_core.rb', line 241

def self.register_auth_menu(partial_path)
  unless partial_path.blank?
    auth_menu_registry << partial_path unless auth_menu_registry.include?(partial_path)
  end
end

.register_db_config_defaults(db_name, defaults) ⇒ Object

Sets the defaults for a database configuration.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/barkest_core.rb', line 94

def self.register_db_config_defaults(db_name, defaults)
  # reset the config cache.
  @db_configs = nil

  db_name = db_name.to_s
  return false if db_name.blank?
  return false if db_name == 'barkest_core'

  # set the defaults.
  @db_config_defaults ||= {}
  @db_config_defaults[db_name.to_sym] = defaults

  true
end

.register_db_seed_path(seed_path) ⇒ Object

Registers a path to be processed by the custom seeds.rb file.



257
258
259
260
261
# File 'lib/barkest_core.rb', line 257

def self.register_db_seed_path(seed_path)
  unless seed_path.blank?
    db_seed_path_registry << partial_path unless db_seed_path_registry.include?(seed_path)
  end
end

Registers a partial view to build up the footer menu.



249
250
251
252
253
# File 'lib/barkest_core.rb', line 249

def self.register_footer_menu(partial_path)
  unless partial_path.blank?
    footer_menu_registry << partial_path unless footer_menu_registry.include?(partial_path)
  end
end

.registry_css_file(css_path) ⇒ Object

Registers a CSS file to include in the application template.



265
266
267
268
269
# File 'lib/barkest_core.rb', line 265

def self.registry_css_file(css_path)
  unless css_path.blank?
    css_registry << css_path unless css_registry.include?(css_path)
  end
end

.registry_js_file(js_path) ⇒ Object

Registers a JS file to include in the application template.



273
274
275
276
277
# File 'lib/barkest_core.rb', line 273

def self.registry_js_file(js_path)
  unless js_path.blank?
    js_registry << js_path unless js_registry.include?(js_path)
  end
end

.request_restartObject

Tells the hosting service (Passenger) that we want to restart the application.



174
175
176
# File 'lib/barkest_core.rb', line 174

def self.request_restart
  FileUtils.touch restart_file
end

.restart_pending?Boolean

Determines if the application is still waiting on a restart to take place.

Returns:

  • (Boolean)


180
181
182
183
184
185
186
# File 'lib/barkest_core.rb', line 180

def self.restart_pending?
  return false unless File.exist?(restart_file)

  request_time = File.mtime(restart_file)

  request_time > start_time
end

Instance Method Details

#app_rootObject

Cache of the application root path for use within the engine.



9
# File 'lib/barkest_core.rb', line 9

mattr_accessor :app_root

#lock_down_usersObject

Determines the accessibility of /users (index) and /users/1 (show) actions.

Set to true to deny access to the users’ list and user profiles for non-admin users. Set to false to allow access to the users’ list for all logged-in users.



16
# File 'lib/barkest_core.rb', line 16

mattr_accessor :lock_down_users