meta_controller 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/meta_controller.rb +45 -0
- data/lib/meta_controller/action_controller.rb +31 -0
- data/lib/meta_controller/adaptors/asset_actions.rb +38 -0
- data/lib/meta_controller/adaptors/create_action.rb +19 -0
- data/lib/meta_controller/adaptors/default_actions.rb +19 -0
- data/lib/meta_controller/adaptors/destroy_action.rb +21 -0
- data/lib/meta_controller/adaptors/destroy_many_action.rb +26 -0
- data/lib/meta_controller/adaptors/edit_action.rb +21 -0
- data/lib/meta_controller/adaptors/index_action.rb +21 -0
- data/lib/meta_controller/adaptors/new_action.rb +19 -0
- data/lib/meta_controller/adaptors/resource_model.rb +34 -0
- data/lib/meta_controller/adaptors/show_action.rb +21 -0
- data/lib/meta_controller/adaptors/update_action.rb +22 -0
- data/lib/meta_controller/definition.rb +40 -0
- data/rails/init.rb +2 -0
- metadata +71 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
$:.unshift(File.dirname(File.expand_path(__FILE__))) unless $:.include?(File.dirname(File.expand_path(__FILE__)))
|
3
|
+
|
4
|
+
module MetaController
|
5
|
+
|
6
|
+
autoload :Definition, 'meta_controller/definition'
|
7
|
+
autoload :ActionController, 'meta_controller/action_controller'
|
8
|
+
|
9
|
+
module Adaptors
|
10
|
+
autoload :NewAction, 'meta_controller/adaptors/new_action'
|
11
|
+
autoload :ShowAction, 'meta_controller/adaptors/show_action'
|
12
|
+
autoload :EditAction, 'meta_controller/adaptors/edit_action'
|
13
|
+
autoload :IndexAction, 'meta_controller/adaptors/index_action'
|
14
|
+
autoload :UpdateAction, 'meta_controller/adaptors/update_action'
|
15
|
+
autoload :CreateAction, 'meta_controller/adaptors/create_action'
|
16
|
+
autoload :DestroyAction, 'meta_controller/adaptors/destroy_action'
|
17
|
+
autoload :DestroyManyAction, 'meta_controller/adaptors/destroy_many_action'
|
18
|
+
|
19
|
+
autoload :DefaultActions, 'meta_controller/adaptors/default_actions'
|
20
|
+
autoload :ResourceModel, 'meta_controller/adaptors/resource_model'
|
21
|
+
autoload :AssetActions, 'meta_controller/adaptors/asset_actions'
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.adaptors
|
25
|
+
@adaptors ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.adaptor(mod)
|
29
|
+
self.adaptors.push mod
|
30
|
+
end
|
31
|
+
|
32
|
+
self.adaptor 'MetaController::Adaptors::IndexAction'
|
33
|
+
self.adaptor 'MetaController::Adaptors::ShowAction'
|
34
|
+
self.adaptor 'MetaController::Adaptors::NewAction'
|
35
|
+
self.adaptor 'MetaController::Adaptors::CreateAction'
|
36
|
+
self.adaptor 'MetaController::Adaptors::EditAction'
|
37
|
+
self.adaptor 'MetaController::Adaptors::UpdateAction'
|
38
|
+
self.adaptor 'MetaController::Adaptors::DestroyAction'
|
39
|
+
self.adaptor 'MetaController::Adaptors::DestroyManyAction'
|
40
|
+
|
41
|
+
self.adaptor 'MetaController::Adaptors::DefaultActions'
|
42
|
+
self.adaptor 'MetaController::Adaptors::ResourceModel'
|
43
|
+
self.adaptor 'MetaController::Adaptors::AssetActions'
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module ActionController
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send :extend, MetaController::ActionController::Macros
|
7
|
+
base.send :include, MetaController::ActionController::InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module Macros
|
11
|
+
|
12
|
+
def definition
|
13
|
+
@controller_definition ||= MetaController::Definition.new(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def define
|
17
|
+
yield(self.definition)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
|
24
|
+
def definition
|
25
|
+
self.class.definition
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module AssetActions
|
5
|
+
|
6
|
+
def has_assets(name)
|
7
|
+
name = name.to_s
|
8
|
+
|
9
|
+
define_controller_method(name, %{
|
10
|
+
#{short_model_name}
|
11
|
+
|
12
|
+
if params[:#{long_model_name}] && params[:#{long_model_name}][:#{name.singularize}]
|
13
|
+
#{short_model_name}.#{name} = [params[:#{long_model_name}][:#{name.singularize}]]
|
14
|
+
elsif params[:#{name}]
|
15
|
+
# to be done
|
16
|
+
end
|
17
|
+
@#{name} = #{short_model_name}.#{name}
|
18
|
+
render :action => '#{name}'
|
19
|
+
})
|
20
|
+
|
21
|
+
define_controller_method("delete_#{name}", %{
|
22
|
+
#{short_model_name}
|
23
|
+
|
24
|
+
selected_assets = params[:#{name}].collect { |id, state| state.to_i == 1 ? id.to_i : nil }
|
25
|
+
selected_assets.compact!
|
26
|
+
|
27
|
+
allocations = #{short_model_name}.#{name.singularize}_allocations.all(:conditions => { :asset_id => selected_assets })
|
28
|
+
allocations.each do |allocation|
|
29
|
+
allocation.destroy
|
30
|
+
end
|
31
|
+
|
32
|
+
redirect_to #{name}_#{long_model_name}_path(#{short_model_name})
|
33
|
+
})
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module CreateAction
|
5
|
+
|
6
|
+
def create_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :after_create_build
|
9
|
+
end
|
10
|
+
define_controller_method(:create, %{
|
11
|
+
build_#{short_model_name}
|
12
|
+
run_callbacks(:after_create_build)
|
13
|
+
render_create(#{short_model_name}, :action_failed => 'form') { #{short_model_name}.save }
|
14
|
+
})
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module DefaultActions
|
5
|
+
|
6
|
+
def default_actions
|
7
|
+
index_action
|
8
|
+
show_action
|
9
|
+
new_action
|
10
|
+
create_action
|
11
|
+
edit_action
|
12
|
+
update_action
|
13
|
+
destroy_action
|
14
|
+
destroy_many_action
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module DestroyAction
|
5
|
+
|
6
|
+
def destroy_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_destroy_find
|
9
|
+
define_callbacks :after_destroy_find
|
10
|
+
end
|
11
|
+
define_controller_method(:destroy, %{
|
12
|
+
run_callbacks(:before_destroy_find)
|
13
|
+
#{short_model_name}
|
14
|
+
run_callbacks(:after_destroy_find)
|
15
|
+
render_destroy(#{short_model_name}) { #{short_model_name}.destroy }
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module DestroyManyAction
|
5
|
+
|
6
|
+
def destroy_many_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_destroy_many_find
|
9
|
+
define_callbacks :after_destroy_many_find
|
10
|
+
end
|
11
|
+
define_controller_method(:destroy_many, %{
|
12
|
+
run_callbacks(:before_destroy_many_find)
|
13
|
+
selected_ids = params[:#{short_model_name.pluralize}].collect { |id, state| state.to_i == 1 ? id.to_i : nil }
|
14
|
+
selected_ids.compact!
|
15
|
+
selected_records = #{model_name}.all(:conditions => { :id => selected_ids })
|
16
|
+
run_callbacks(:after_destroy_many_find)
|
17
|
+
|
18
|
+
selected_records.each { |r| r.destroy }
|
19
|
+
|
20
|
+
redirect_to #{long_model_name.pluralize}_path
|
21
|
+
})
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module EditAction
|
5
|
+
|
6
|
+
def edit_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_edit_find
|
9
|
+
define_callbacks :after_edit_find
|
10
|
+
end
|
11
|
+
define_controller_method(:edit, %{
|
12
|
+
run_callbacks(:before_edit_find)
|
13
|
+
#{short_model_name}
|
14
|
+
run_callbacks(:after_edit_find)
|
15
|
+
render_any #{short_model_name}, :action => 'form'
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module IndexAction
|
5
|
+
|
6
|
+
def index_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_index_find
|
9
|
+
define_callbacks :after_index_find
|
10
|
+
end
|
11
|
+
define_controller_method(:index, %{
|
12
|
+
run_callbacks(:before_index_find)
|
13
|
+
#{short_model_name.pluralize}
|
14
|
+
run_callbacks(:after_index_find)
|
15
|
+
render_any #{short_model_name.pluralize}
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module NewAction
|
5
|
+
|
6
|
+
def new_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :after_new_build
|
9
|
+
end
|
10
|
+
define_controller_method(:new, %{
|
11
|
+
build_#{short_model_name}
|
12
|
+
run_callbacks(:after_new_build)
|
13
|
+
render_any #{short_model_name}, :action => 'form'
|
14
|
+
})
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module ResourceModel
|
5
|
+
|
6
|
+
def model_name(name=nil)
|
7
|
+
if name
|
8
|
+
@model_name = name
|
9
|
+
@short_model_name = nil
|
10
|
+
@long_model_name = nil
|
11
|
+
controller_eval do
|
12
|
+
deals_with name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
@model_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def short_model_name
|
19
|
+
if self.model_name and !@short_model_name
|
20
|
+
@short_model_name = self.model_name.split('::').last.underscore
|
21
|
+
end
|
22
|
+
@short_model_name
|
23
|
+
end
|
24
|
+
|
25
|
+
def long_model_name
|
26
|
+
if self.model_name and !@long_model_name
|
27
|
+
@long_model_name = self.model_name.gsub('::', '_').underscore
|
28
|
+
end
|
29
|
+
@long_model_name
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module ShowAction
|
5
|
+
|
6
|
+
def show_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_show_find
|
9
|
+
define_callbacks :after_show_find
|
10
|
+
end
|
11
|
+
define_controller_method(:show, %{
|
12
|
+
run_callbacks(:before_show_find)
|
13
|
+
#{short_model_name}
|
14
|
+
run_callbacks(:after_show_find)
|
15
|
+
render_any #{short_model_name}, :action => 'form'
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
module Adaptors
|
4
|
+
module UpdateAction
|
5
|
+
|
6
|
+
def update_action
|
7
|
+
controller_eval do
|
8
|
+
define_callbacks :before_update_find
|
9
|
+
define_callbacks :after_update_find
|
10
|
+
end
|
11
|
+
define_controller_method(:update, %{
|
12
|
+
run_callbacks(:before_update_find)
|
13
|
+
#{short_model_name}
|
14
|
+
run_callbacks(:after_update_find)
|
15
|
+
render_update(#{short_model_name}, :action_failed => 'form') {
|
16
|
+
#{short_model_name}.update_attributes(params[:#{long_model_name}]) }
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
module MetaController
|
3
|
+
class Definition
|
4
|
+
|
5
|
+
attr_reader :controller_class
|
6
|
+
|
7
|
+
def initialize(controller_class)
|
8
|
+
@controller_class = controller_class
|
9
|
+
|
10
|
+
controller_eval do
|
11
|
+
include ::ActiveSupport::Callbacks
|
12
|
+
private :run_callbacks
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
metaclass = (class << self ; self ; end)
|
17
|
+
MetaController.adaptors.each do |adaptor|
|
18
|
+
metaclass.send(:include, adaptor.to_s.constantize)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def define_controller_method(name, code=nil, &block)
|
23
|
+
if !block.nil?
|
24
|
+
controller_class.define_method(name, &block)
|
25
|
+
elsif !code.nil?
|
26
|
+
controller_class.module_eval %{def #{name} ; #{code} ; end}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def controller_eval(code=nil, &block)
|
31
|
+
if block
|
32
|
+
controller_class.instance_eval(&block)
|
33
|
+
else
|
34
|
+
controller_class.instance_eval(code)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meta_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Menke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-19 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Define your controllers with an easy DSL
|
17
|
+
email: simon.menke@gmail.com
|
18
|
+
engine_dependencies: {}
|
19
|
+
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- lib/meta_controller/action_controller.rb
|
28
|
+
- lib/meta_controller/adaptors/asset_actions.rb
|
29
|
+
- lib/meta_controller/adaptors/create_action.rb
|
30
|
+
- lib/meta_controller/adaptors/default_actions.rb
|
31
|
+
- lib/meta_controller/adaptors/destroy_action.rb
|
32
|
+
- lib/meta_controller/adaptors/destroy_many_action.rb
|
33
|
+
- lib/meta_controller/adaptors/edit_action.rb
|
34
|
+
- lib/meta_controller/adaptors/index_action.rb
|
35
|
+
- lib/meta_controller/adaptors/new_action.rb
|
36
|
+
- lib/meta_controller/adaptors/resource_model.rb
|
37
|
+
- lib/meta_controller/adaptors/show_action.rb
|
38
|
+
- lib/meta_controller/adaptors/update_action.rb
|
39
|
+
- lib/meta_controller/definition.rb
|
40
|
+
- lib/meta_controller.rb
|
41
|
+
- rails/init.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/simonmenke/meta_controller
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Define your controllers with an easy DSL
|
70
|
+
test_files: []
|
71
|
+
|