lib/checkpoint/railtie.rb in checkpoint-0.2.2 vs lib/checkpoint/railtie.rb in checkpoint-1.0.0

- old
+ new

@@ -1,84 +1,105 @@ -require 'rails' +# frozen_string_literal: true -class Checkpoint::Railtie < ::Rails::Railtie - config.before_initialize do - class ::ActionController::Base - - def self.authorise_controllers_blocks - if @authorise_controllers_blocks.nil? - @authorise_controllers_blocks = {} - end - @authorise_controllers_blocks +module Checkpoint + # Railtie to hook Checkpoint into Rails applications. + # + # This does three things at present: + # + # 1. Loads our rake tasks, so you can run checkpoint:migrate from the app. + # 2. Pulls the Rails database information off of the ActiveRecord + # connection and puts it on Checkpoint::DB.config before any application + # initializers are run. + # 3. Sets up the Checkpoint database connection after application + # initializers have run, if it has not already been done and we are not + # running as a Rake task. This condition is key because when we are in + # rails server or console, we want to initialize!, but when we are in + # a rake task to update the database, we have to let it connect, but + # not initialize. + class Railtie < Rails::Railtie + railtie_name :checkpoint + + class << self + # Register a callback to run before anything in 'config/initializers' runs. + # The block will get a reference to Checkpoint::DB.config as its only parameter. + def before_initializers(&block) + before_blocks << block end - - def self.authorise(arg1, &block) - - if block.nil? - block = lambda {|c| true} - end - - to_regexp = lambda do |pattern| - if arg1.class.to_s == 'Regexp' - arg1 - else - Regexp.new('\A' + pattern.to_s.gsub(/[^\*]/){|char| Regexp.quote(char)}.gsub(/\*/){|| ".*?"} + '\Z') - end - end - - patterns = [] - if arg1.class.to_s == 'Array' - arg1.each {|pattern| patterns.push to_regexp.call(pattern) } - else - patterns.push to_regexp.call(arg1) - end - - authorise_controllers_blocks = ::ApplicationController.authorise_controllers_blocks - - patterns.each do |pattern| - if authorise_controllers_blocks [pattern].nil? - authorise_controllers_blocks[pattern] = [] - end - authorise_controllers_blocks[pattern].push(block) - end + + # Register a callback to run after anything in 'config/initializers' runs. + # The block will get a reference to Checkpoint::DB.config as its only parameter. + # Checkpoint::DB.initialize! will not have been automatically called at this + # point, so this is an opportunity to do so if an initializer has not. + def after_initializers(&block) + after_blocks << block end - - #for our american friends - def self.authorize(arg1, &block) - authorise(arg1, &block) + + # Register a callback to run when Checkpoint is ready and fully initialized. + # This will happen once in production, and on each request in development. + # If you need to do something once in development, you can choose between + # keeping a flag or using the after_initializers. + def when_checkpoint_is_ready(&block) + ready_blocks << block end - - def authorised? - action = "#{self.class.to_s}::#{params[:action]}" - ::ApplicationController.authorise_controllers_blocks.each do |pattern, blocks| - if action.match pattern - blocks.each do |block| - if instance_eval(&block) - return true - end - end - end - end - false + + def before_blocks + @before ||= [] end - def access_denied - logger.info "\n\n-----------------------------------------------" - logger.info " (401) Access Denied!" - logger.info " * see the above request for more info" - logger.info "-----------------------------------------------\n\n" - render :text => "Access Denied", :status => 401 + def after_blocks + @after ||= [] end - def check_authorized - if !authorised? - access_denied - end + def ready_blocks + @ready ||= [] end - - before_filter do |controller| - check_authorized + + def under_rake! + @rake = true end - + + def under_rake? + @rake ||= false + end + end + + # This runs before anything in 'config/initializers' runs. + initializer "checkpoint.before_initializers", before: :load_config_initializers do + config = Checkpoint::DB.config + unless config.url + opts = ActiveRecord::Base.connection.instance_variable_get(:@config).dup + opts.delete(:flags) + config[:opts] = opts + end + + Railtie.before_blocks.each do |block| + block.call(config.to_h) + end + end + + # This runs after everything in 'config/initializers' runs. + initializer "checkpoint.after_initializers", after: :load_config_initializers do + config = Checkpoint::DB.config + Railtie.after_blocks.each do |block| + block.call(config.to_h) + end + end + + # This runs before any block registered under a `config.to_prepare`, which + # could be in plugins or initializers that want to use a fully configured + # Checkpoint instance. The `to_prepare` hook is run once at the start of a + # production instance and for every request in development (unless caching + # is turned on so there is no reloading). + initializer "checkpoint.ready", after: :finisher_hook do + Checkpoint::DB.initialize! unless Railtie.under_rake? + + Railtie.ready_blocks.each do |block| + block.call(Checkpoint::DB.db) + end + end + + rake_tasks do + Railtie.under_rake! + load "tasks/migrate.rake" end end end