module Netfira::WebConnect

todo needs comment

Constants

PLATFORM_AND_VERSION
ROOT
VERSION

Public Class Methods

amqp_uri() click to toggle source
# File lib/netfira/web_connect.rb, line 77
def self.amqp_uri
  @config.amqp
end
anonymous_shop() click to toggle source
# File lib/netfira/web_connect.rb, line 57
def self.anonymous_shop
  @anonymous_shop ||= Models::Shop.find(0)
end
authenticator() click to toggle source
# File lib/netfira/web_connect.rb, line 53
def self.authenticator
  @config.authenticator
end
configure(input = nil, **options) { |config| ... } click to toggle source
# File lib/netfira/web_connect.rb, line 12
def self.configure(input = nil, **options)
  if Hash === input
    @config = Configuration.new(input)
  elsif String === input
    @config = Configuration.new(YAML.parse input)
  elsif input.respond_to? :read
    @config = Configuration.new(YAML.parse_stream input)
  elsif input.nil?
    @config ||= Configuration.new
  else
    raise "You cannot configure WebConnect with an instance of #{input.class}"
  end
  @config.materialize_when_db_changed = !options[:skip_materialization]
  yield @config if block_given?
  Paperclip::Attachment.default_options[:preserve_files] = paranoia?
end
db_table_l10n_suffix(table_name = '') click to toggle source
# File lib/netfira/web_connect.rb, line 33
def self.db_table_l10n_suffix(table_name = '')
  :"#{table_name}_l10n"
end
db_table_prefix(string = '') click to toggle source
# File lib/netfira/web_connect.rb, line 29
def self.db_table_prefix(string = '')
  [@config.db_table_prefix, string].join.to_sym
end
dispatch_event(timing, event, *args) { || ... } click to toggle source
# File lib/netfira/web_connect/events.rb, line 7
def self.dispatch_event(timing, event, *args)

  # Allow multiple timings to be dispatched simultaneously, e.g. [:on, :after]
  return timing.each{ |t| dispatch_event(t, event, *args) { yield } } if timing.respond_to? :each

  # The method to invoke, e.g. :on_update_product
  target = :"#{timing}_#{event}"

  # Delegates that respond to this method
  delegates = event_delegates.select { |d| d.respond_to? target }

  # 'Around' events get special treatment
  if timing == :around
    chain_runner = -> do
      delegate = delegates.shift
      if delegate
        delegate.__send__ target, *args, &chain_runner
      else
        yield
      end
    end
    chain_runner.call
  else
    delegates.map { |d| d.__send__ target, *args }
  end
end
event_delegates() click to toggle source
# File lib/netfira/web_connect/events.rb, line 3
def self.event_delegates
  @event_delegates ||= []
end
file_store() click to toggle source
# File lib/netfira/web_connect.rb, line 49
def self.file_store
  @config.file_store || {}
end
handles_event?(event) click to toggle source
# File lib/netfira/web_connect/events.rb, line 34
def self.handles_event?(event)
  target = :"on_#{event}"
  !!event_delegates.find { |x| x.respond_to? target }
end
http_notifications() click to toggle source
# File lib/netfira/web_connect.rb, line 73
def self.http_notifications
  @config.http_notifications || :none
end
logger() click to toggle source
# File lib/netfira/web_connect.rb, line 41
def self.logger
  @config.logger || null_logger
end
migrate() click to toggle source
# File lib/netfira/web_connect/migration/base_methods.rb, line 3
def self.migrate
  force_db_connection { ActiveRecord::Migrator.migrate migrations_dir }
end
needs_migration?() click to toggle source
# File lib/netfira/web_connect/migration/base_methods.rb, line 11
def self.needs_migration?
  original = ActiveRecord::Migrator.migrations_paths
  ActiveRecord::Migrator.migrations_paths = [migrations_dir]
  result = force_db_connection { ActiveRecord::Migrator.needs_migration? }
  ActiveRecord::Migrator.migrations_paths = original
  result
end
paranoia?() click to toggle source
# File lib/netfira/web_connect.rb, line 69
def self.paranoia?
  !!@config.paranoia
end
rollback() click to toggle source
# File lib/netfira/web_connect/migration/base_methods.rb, line 7
def self.rollback
  force_db_connection { ActiveRecord::Migrator.rollback migrations_dir }
end
schema() click to toggle source
# File lib/netfira/web_connect.rb, line 61
def self.schema
  @schema ||= Schema.new if @config
end
schema_migrations_table_name() click to toggle source
# File lib/netfira/web_connect.rb, line 37
def self.schema_migrations_table_name
  db_table_prefix('schema_migrations').to_s
end
session_lifetime() click to toggle source
# File lib/netfira/web_connect.rb, line 65
def self.session_lifetime
  @config.session_lifetime
end
system_locale() click to toggle source
# File lib/netfira/web_connect.rb, line 45
def self.system_locale
  @system_locale ||= Locale.current.to_simple.to_s
end

Private Class Methods

force_db_connection() { || ... } click to toggle source

This method tricks ActiveRecord into using the connection for Netfira::WebConnect::Model instead of the ActiveRecord::Base connection, which it's hard-wired to do for migrations and other tasks. Two methods are backed up and replaced, then restored after yield. Those methods are identical to the originals, except they specify `Model` in place of `self`.

# File lib/netfira/web_connect/migration/base_methods.rb, line 29
def self.force_db_connection

  # Save the original methods
  originals = [:connection_pool, :retrieve_connection, :schema_migrations_table_name].map do |name|
    [name, ActiveRecord::Base.method(name)]
  end.to_h

  # Save the original schema migration table
  migration_table = ActiveRecord::SchemaMigration.all.table
  original_migration_table = [migration_table.name, migration_table.engine]

  # Sub in our own migration table details
  migration_table.name = Netfira::WebConnect.schema_migrations_table_name
  migration_table.engine = Model

  # Add some new methods
  class << ActiveRecord::Base
    def connection_pool
      connection_handler.retrieve_connection_pool(Model) or raise ActiveRecord::ConnectionNotEstablished
    end
    def retrieve_connection
      connection_handler.retrieve_connection(Model)
    end
    def schema_migrations_table_name
      Netfira::WebConnect.schema_migrations_table_name
    end
  end

  # Run the given block
  result = yield

  # Restore the original migration table name
  migration_table.name, migration_table.engine = original_migration_table

  # Restore the original methods
  originals.each { |name, method| ActiveRecord::Base.define_singleton_method name, method }

  # Return the result of the given block
  result
end
migrations_dir() click to toggle source
# File lib/netfira/web_connect/migration/base_methods.rb, line 21
def self.migrations_dir
  @migrations_dir ||= ROOT.join('web_connect/db_schema').to_s
end
null_logger() click to toggle source
# File lib/netfira/web_connect.rb, line 83
def self.null_logger
  @null_logger ||= Logger.new nil
end