Sha256: 45fc4316bc99d746cf5db84ff1b76eb8306a28afc502c087b4fd6ceb5e7c29a1

Contents?: true

Size: 1.7 KB

Versions: 7

Compression:

Stored size: 1.7 KB

Contents

require "outpost/engine"

require 'active_record'
require 'action_controller'
require 'action_view'
require 'action_view/helpers/form_builder'

require 'outpost/config'

module Outpost
  extend ActiveSupport::Autoload

  OBJ_KEY_REGEX = %r{([^-]+)-(\d+)}

  DESCENDING  = "DESC"
  ASCENDING   = "ASC"

  autoload :Controller
  autoload :Model
  autoload :Hook
  autoload :Helpers
  autoload :List
  autoload :Breadcrumb, 'outpost/breadcrumbs'
  autoload :Breadcrumbs

  class << self
    attr_writer :config
    def config
      @config ||= Outpost::Config.new
    end

    # TODO can we cache this in development?
    def user_class
      config.user_class.constantize
    end

    #--------------------
    # Convert key from "app/model:id" to AppModel.find_by_id(id)
    def obj_by_key(key)
      if match = match_key(key)
        model = model_classes[match[1]]
        model.find_by_id(match[2]) if model
      end
    end

    #--------------------
    # Same as #obj_by_key, but raises ActiveRecord::RecordNotFound
    # if no object is found or if key doesn't match.
    def obj_by_key!(key)
      obj_by_key(key) or raise ActiveRecord::RecordNotFound
    end


    private

    def match_key(key)
      key.to_s.match(OBJ_KEY_REGEX)
    end

    #--------------------

    def model_classes
      @model_classes ||= begin
        klasses = {}

        Outpost.config.registered_models.each do |name|
          klass = name.constantize
          klasses.merge!(klass.content_key => klass)
        end

        klasses
      end
    end
  end
end

if defined?(ActiveRecord::Base)
  ActiveRecord::Base.send :include, Outpost::Model
end

if defined?(ActionController::Base)
  ActionController::Base.send :include, Outpost::Controller
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
outpost-cms-0.1.4 lib/outpost.rb
outpost-cms-0.1.3 lib/outpost.rb
outpost-cms-0.1.2 lib/outpost.rb
outpost-cms-0.1.1 lib/outpost.rb
outpost-cms-0.1.0 lib/outpost.rb
outpost-cms-0.0.5 lib/outpost.rb
outpost-cms-0.0.4 lib/outpost.rb