Class: Databound::Controller

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

Class Method Summary collapse

Class Method Details

.as_constant_string(name) ⇒ Object



48
49
50
# File 'lib/databound/controller.rb', line 48

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

.create(name, resource, opts) ⇒ Object



8
9
10
11
# File 'lib/databound/controller.rb', line 8

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

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/databound/controller.rb', line 32

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



28
29
30
# File 'lib/databound/controller.rb', line 28

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

.find(path) ⇒ Object



44
45
46
# File 'lib/databound/controller.rb', line 44

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

.find_or_create(name, resource, opts) ⇒ Object



4
5
6
# File 'lib/databound/controller.rb', line 4

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

.new(resource, opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/databound/controller.rb', line 13

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