Class: Databound::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/databound/controller.rb

Class Method Summary collapse

Class Method Details

.add_application_controller_configs!Object



4
5
6
7
8
9
10
11
12
# File 'lib/databound/controller.rb', line 4

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

.as_constant_string(name) ⇒ Object



58
59
60
# File 'lib/databound/controller.rb', line 58

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

.create(name, resource, opts) ⇒ Object



18
19
20
21
# File 'lib/databound/controller.rb', line 18

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

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/databound/controller.rb', line 42

def exists?(path)
  name_error = false

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

  !name_error
end

.fallback_model(resource) ⇒ Object



38
39
40
# File 'lib/databound/controller.rb', line 38

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

.find(path) ⇒ Object



54
55
56
# File 'lib/databound/controller.rb', line 54

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

.find_or_create(name, resource, opts) ⇒ Object



14
15
16
# File 'lib/databound/controller.rb', line 14

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

.new(resource, opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/databound/controller.rb', line 23

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