Module: Intermodal::DSL::Controllers

Extended by:
ActiveSupport::Concern
Included in:
API
Defined in:
lib/intermodal/dsl/controllers.rb

Instance Method Summary collapse

Instance Method Details

#_controller_name(name) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/intermodal/dsl/controllers.rb', line 98

def _controller_name(name)
  case name
  when String then name.to_s
  when Symbol then "#{name.to_s.camelize}Controller"
  else
    raise "resources require String or Symbol"
  end
end

#_create_controller(collection_name, controller_name, customizations, options = {}, &blk) ⇒ Object

API: private



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/intermodal/dsl/controllers.rb', line 77

def _create_controller(collection_name, controller_name, customizations, options = {},  &blk)
  _ancestor = options[:ancestor] || ApplicationController
  _namespace = options[:namespace] #|| Object

  _unload_controller_if_exists(controller_name, _namespace)

  _namespace ||= Object

  controller = Class.new(_ancestor)
  _namespace.const_set(controller_name, controller)

  controller.collection_name = collection_name
  controller.model = options[:model] if options[:model]
  controller.parent_resource_name = options[:parent_resource_name] if options[:parent_resource_name]
  controller.parent_model = parent_model if options[:parent_model]
  controller.class_eval(&customizations) if customizations
  controller.api = options[:api] if options[:api]

  controller
end

#_module(module_name) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/intermodal/dsl/controllers.rb', line 107

def _module(module_name)
  module_name = module_name.to_s.camelize
  if Object.const_defined?(module_name)
    module_name.constantize
  else
    Object.const_set(module_name, Module.new)
  end
end

#_unload_controller_if_exists(controller_name, _namespace = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/intermodal/dsl/controllers.rb', line 116

def _unload_controller_if_exists(controller_name, _namespace = nil)
  full_controller_name = [_namespace, controller_name].join('::')

  # I found no other good way to detect this without using eval that would work
  # for both 1.8.7 and 1.9.2
  (_namespace || Object).send(:remove_const, controller_name) if eval "defined? #{full_controller_name}" # Unload existing class
end

#controllers(&blk) ⇒ Object



18
19
20
# File 'lib/intermodal/dsl/controllers.rb', line 18

def controllers(&blk)
  self.controller_definitions = blk
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/intermodal/dsl/controllers.rb', line 46

def link_resources_from(parent_name, options = {}, &blk)
  _model = options[:model]
  _parent_model = options[:parent_model]
  _parent_resource_name = parent_name.to_s.singularize.to_sym
  _target_resource_name = options[:to]
  _namespace = _module(parent_name)
  controller_name = _controller_name(_target_resource_name)

  collection_name = "#{_target_resource_name.to_s.singularize}_ids"

  customize_linking_resource = proc do
    let(:model) { _model }
    let(:parent_resource_name) { _parent_resource_name }
    let(:target_resource_name) { _target_resource_name }
  end

  controller = _create_controller(collection_name, controller_name, customize_linking_resource,
                                  :ancestor => Intermodal::LinkingResourceController,
                                  :namespace => _namespace,
                                  :model => _model,
                                  :parent_resource => _parent_resource_name,
                                  :parent_model => _parent_model,
                                  :api => self)
  controller.instance_eval(&blk) if blk
end

#load_controllers!Object



72
73
74
# File 'lib/intermodal/dsl/controllers.rb', line 72

def load_controllers!
  self.controller_definitions.call
end

#nested_resources(parent_name, name, options = {}, &blk) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/intermodal/dsl/controllers.rb', line 31

def nested_resources(parent_name, name, options = {}, &blk)
  _parent_model = options[:parent_model]
  _parent_resource_name = parent_name.to_s.singularize.to_sym
  _namespace = _module(parent_name)
  controller_name = _controller_name(name)

  _create_controller(name, controller_name, blk,
                     :ancestor => Intermodal::NestedResourceController,
                     :namespace => _namespace,
                     :model => options[:model],
                     :parent_resource_name => _parent_resource_name,
                     :parent_model => _parent_model,
                     :api => self)
end

#resources(name, options = {}, &blk) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/intermodal/dsl/controllers.rb', line 22

def resources(name, options = {}, &blk)
  controller_name = _controller_name(name)
  _create_controller(name, controller_name, blk,
                     api:       self,
                     model:     options[:model],
                     ancestor:  Intermodal::ResourceController,
                     namespace: options[:namespace])
end