Sha256: 2f9d8aabcdeaac17bb4f5ba2ecf2d61a74cb5d72de12e6892309e3ced7b36d41

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

module Databound
  class Controller
    class << self
      def add_application_controller_configs!
        def ApplicationController.databound(model = nil, &block)
          include Databound

          send(:define_method, :databound_config) do
            Databound::Config.new(block, model)
          end
        end
      end

      def find_or_create(name, resource, opts)
        find(name) || create(name, resource, opts)
      end

      def create(name, resource, opts)
        opts ||= {}
        Object.const_set(as_constant_string(name), new(resource, opts))
      end

      def new(resource, opts)
        model_name = opts.delete(:model) || fallback_model(resource)

        result = Class.new(ApplicationController)
        result.send(:databound) do
          model model_name

          opts.each do |key, value|
            send(key, *value)
          end
        end

        result
      end

      def fallback_model(resource)
        resource.to_s.classify.underscore.to_sym
      end

      def exists?(path)
        name_error = false

        begin
          as_constant_string(path).constantize
        rescue NameError
          name_error = true
        end

        !name_error
      end

      def find(path)
        as_constant_string(path).constantize if exists?(path)
      end

      def as_constant_string(name)
        "#{name.camelize}Controller"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
databound-3.0.2 lib/databound/controller.rb
databound-3.0.1 lib/databound/controller.rb
databound-3.0.0 lib/databound/controller.rb