toy-locomotive 0.1.0 → 0.1.1
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/toy-locomotive/attributes/chain.rb +65 -65
- data/lib/toy-locomotive/attributes/model.rb +23 -23
- data/lib/toy-locomotive/attributes/observer.rb +43 -43
- data/lib/toy-locomotive/auto_views/form.rb +54 -54
- data/lib/toy-locomotive/auto_views/show.rb +19 -19
- data/lib/toy-locomotive/initializer.rb +20 -20
- data/lib/toy-locomotive/resources/controller.rb +102 -102
- data/lib/toy-locomotive/router/block.rb +13 -13
- data/lib/toy-locomotive/router/controller.rb +134 -134
- data/lib/toy-locomotive/router/model.rb +40 -40
- data/lib/toy-locomotive/version.rb +1 -1
- data/readme +15 -15
- data/spec/lib/attribute_chain_spec.rb +40 -40
- data/spec/lib/attribute_model_spec.rb +10 -10
- data/spec/lib/resources_controller_spec.rb +39 -39
- data/spec/lib/router_controller_spec.rb +108 -108
- data/spec/lib/router_model_spec.rb +49 -49
- data/spec/lib/toy_locomotive_spec.rb +7 -7
- data/spec/spec_helper.rb +6 -6
- data/spec/support/controllers.rb +30 -30
- data/spec/support/models.rb +16 -16
- data/spec/support/schema.rb +19 -19
- data/spec/support/seeds.rb +1 -1
- metadata +11 -31
@@ -1,65 +1,65 @@
|
|
1
|
-
module ToyLocomotive::Attributes
|
2
|
-
class AttributeChain
|
3
|
-
|
4
|
-
attr_accessor :column, :parent, :_as, :_helper, :_options
|
5
|
-
|
6
|
-
def initialize column, parent
|
7
|
-
@column = column
|
8
|
-
@parent = parent
|
9
|
-
@_as = :string
|
10
|
-
end
|
11
|
-
|
12
|
-
def as value
|
13
|
-
@_as = value
|
14
|
-
@parent.send @_as, @column if [:has_many, :has_and_belongs_to_many, :has_one, :belongs_to].include? @_as
|
15
|
-
self
|
16
|
-
end
|
17
|
-
|
18
|
-
def options *args
|
19
|
-
@_options = (args.any? && args.first.is_a?(Array)) ? args.first : args
|
20
|
-
self
|
21
|
-
end
|
22
|
-
|
23
|
-
def presence bool
|
24
|
-
parent.send :validates_presence_of, column
|
25
|
-
self
|
26
|
-
end
|
27
|
-
|
28
|
-
def uniqueness value
|
29
|
-
return parent.send :validates_uniqueness_of, column if value == true
|
30
|
-
parent.send :validates_uniqueness_of, column, {scope: value}
|
31
|
-
self
|
32
|
-
end
|
33
|
-
|
34
|
-
def helper value
|
35
|
-
@_helper = value
|
36
|
-
self
|
37
|
-
end
|
38
|
-
|
39
|
-
def to_table_column
|
40
|
-
return nil if skip_table_column?
|
41
|
-
return :"#{@column}_id" if @_as == :belongs_to
|
42
|
-
@column
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_table_type
|
46
|
-
return nil if skip_table_column?
|
47
|
-
return :integer if @_as == :belongs_to
|
48
|
-
@_as
|
49
|
-
end
|
50
|
-
|
51
|
-
def skip_table_column?
|
52
|
-
[:has_many, :has_and_belongs_to_many, :has_one].include? @_as
|
53
|
-
end
|
54
|
-
|
55
|
-
def to_helper
|
56
|
-
return @_helper if @_helper
|
57
|
-
return :text_area if @_as == :text
|
58
|
-
return :hidden_field if @column == :id || @_as == :belongs_to
|
59
|
-
return :check_box if @_as == :boolean
|
60
|
-
return :date_select if @_as == :date
|
61
|
-
:text_field
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
65
|
-
end
|
1
|
+
module ToyLocomotive::Attributes
|
2
|
+
class AttributeChain
|
3
|
+
|
4
|
+
attr_accessor :column, :parent, :_as, :_helper, :_options
|
5
|
+
|
6
|
+
def initialize column, parent
|
7
|
+
@column = column
|
8
|
+
@parent = parent
|
9
|
+
@_as = :string
|
10
|
+
end
|
11
|
+
|
12
|
+
def as value
|
13
|
+
@_as = value
|
14
|
+
@parent.send @_as, @column if [:has_many, :has_and_belongs_to_many, :has_one, :belongs_to].include? @_as
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def options *args
|
19
|
+
@_options = (args.any? && args.first.is_a?(Array)) ? args.first : args
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def presence bool
|
24
|
+
parent.send :validates_presence_of, column
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def uniqueness value
|
29
|
+
return parent.send :validates_uniqueness_of, column if value == true
|
30
|
+
parent.send :validates_uniqueness_of, column, {scope: value}
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def helper value
|
35
|
+
@_helper = value
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_table_column
|
40
|
+
return nil if skip_table_column?
|
41
|
+
return :"#{@column}_id" if @_as == :belongs_to
|
42
|
+
@column
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_table_type
|
46
|
+
return nil if skip_table_column?
|
47
|
+
return :integer if @_as == :belongs_to
|
48
|
+
@_as
|
49
|
+
end
|
50
|
+
|
51
|
+
def skip_table_column?
|
52
|
+
[:has_many, :has_and_belongs_to_many, :has_one].include? @_as
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_helper
|
56
|
+
return @_helper if @_helper
|
57
|
+
return :text_area if @_as == :text
|
58
|
+
return :hidden_field if @column == :id || @_as == :belongs_to
|
59
|
+
return :check_box if @_as == :boolean
|
60
|
+
return :date_select if @_as == :date
|
61
|
+
:text_field
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -1,23 +1,23 @@
|
|
1
|
-
module ToyLocomotive::Attributes::Model
|
2
|
-
module ClassMethods
|
3
|
-
|
4
|
-
mattr_accessor :toy_attributes
|
5
|
-
self.toy_attributes = []
|
6
|
-
|
7
|
-
def attribute value
|
8
|
-
tattr = ToyLocomotive::Attributes::AttributeChain.new value, self
|
9
|
-
self.toy_attributes << tattr
|
10
|
-
tattr
|
11
|
-
end
|
12
|
-
|
13
|
-
def attributes
|
14
|
-
toy_attributes.select{|a| a.parent == self}
|
15
|
-
end
|
16
|
-
|
17
|
-
def use_toy_attributes?
|
18
|
-
true
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
ActiveRecord::Base.extend ToyLocomotive::Attributes::Model::ClassMethods
|
1
|
+
module ToyLocomotive::Attributes::Model
|
2
|
+
module ClassMethods
|
3
|
+
|
4
|
+
mattr_accessor :toy_attributes
|
5
|
+
self.toy_attributes = []
|
6
|
+
|
7
|
+
def attribute value
|
8
|
+
tattr = ToyLocomotive::Attributes::AttributeChain.new value, self
|
9
|
+
self.toy_attributes << tattr
|
10
|
+
tattr
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
toy_attributes.select{|a| a.parent == self}
|
15
|
+
end
|
16
|
+
|
17
|
+
def use_toy_attributes?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
ActiveRecord::Base.extend ToyLocomotive::Attributes::Model::ClassMethods
|
@@ -1,43 +1,43 @@
|
|
1
|
-
module ToyLocomotive
|
2
|
-
class AttributeObserver
|
3
|
-
|
4
|
-
attr_accessor :attribute
|
5
|
-
|
6
|
-
def initialize model
|
7
|
-
return unless model.respond_to?(:use_toy_attributes?) && model.use_toy_attributes?
|
8
|
-
@model = model
|
9
|
-
unless ActiveRecord::Base.connection.table_exists? @model.table_name
|
10
|
-
Class.new(ActiveRecord::Migration).create_table(@model.table_name.to_sym) do |t|
|
11
|
-
t.timestamps
|
12
|
-
end
|
13
|
-
end
|
14
|
-
@model.attributes.each do |attribute|
|
15
|
-
set_attribute(attribute)
|
16
|
-
@model.attr_accessible attribute.column
|
17
|
-
@model.attr_accessible :"#{attribute.column}_id" if attribute._as == :belongs_to
|
18
|
-
end
|
19
|
-
@model.reset_column_information
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_attribute attribute
|
23
|
-
return if attribute.skip_table_column?
|
24
|
-
add_attribute attribute
|
25
|
-
update_attribute attribute
|
26
|
-
end
|
27
|
-
|
28
|
-
def add_attribute attribute
|
29
|
-
unless @model.column_names.include? attribute.to_table_column.to_s
|
30
|
-
Class.new(ActiveRecord::Migration).add_column @model.table_name.to_sym, attribute.to_table_column, attribute.to_table_type
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def update_attribute attribute
|
35
|
-
column = @model.columns.select{|c| c.name == attribute.to_table_column.to_s}.first
|
36
|
-
if column && column.type != attribute.to_table_type
|
37
|
-
Class.new(ActiveRecord::Migration).change_column @model.table_name, attribute.to_table_column, attribute.to_table_type
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
1
|
+
module ToyLocomotive
|
2
|
+
class AttributeObserver
|
3
|
+
|
4
|
+
attr_accessor :attribute
|
5
|
+
|
6
|
+
def initialize model
|
7
|
+
return unless model.respond_to?(:use_toy_attributes?) && model.use_toy_attributes?
|
8
|
+
@model = model
|
9
|
+
unless ActiveRecord::Base.connection.table_exists? @model.table_name
|
10
|
+
Class.new(ActiveRecord::Migration).create_table(@model.table_name.to_sym) do |t|
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@model.attributes.each do |attribute|
|
15
|
+
set_attribute(attribute)
|
16
|
+
@model.attr_accessible attribute.column
|
17
|
+
@model.attr_accessible :"#{attribute.column}_id" if attribute._as == :belongs_to
|
18
|
+
end
|
19
|
+
@model.reset_column_information
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_attribute attribute
|
23
|
+
return if attribute.skip_table_column?
|
24
|
+
add_attribute attribute
|
25
|
+
update_attribute attribute
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_attribute attribute
|
29
|
+
unless @model.column_names.include? attribute.to_table_column.to_s
|
30
|
+
Class.new(ActiveRecord::Migration).add_column @model.table_name.to_sym, attribute.to_table_column, attribute.to_table_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_attribute attribute
|
35
|
+
column = @model.columns.select{|c| c.name == attribute.to_table_column.to_s}.first
|
36
|
+
if column && column.type != attribute.to_table_type
|
37
|
+
Class.new(ActiveRecord::Migration).change_column @model.table_name, attribute.to_table_column, attribute.to_table_type
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -1,54 +1,54 @@
|
|
1
|
-
module ToyLocomotive::AutoViews
|
2
|
-
def auto_form_for args
|
3
|
-
if block_given?
|
4
|
-
form = ToyLocomotive::AutoViews::AutoForm.new
|
5
|
-
yield(form)
|
6
|
-
end
|
7
|
-
form_for args do |f|
|
8
|
-
arg = args.class == Array ? args.last : args
|
9
|
-
klass = arg.class
|
10
|
-
html = ''
|
11
|
-
klass.attributes.each do |attr|
|
12
|
-
next if attr.skip_table_column?
|
13
|
-
if attr.to_helper == :hidden_field
|
14
|
-
html += f.send :hidden_field, attr.to_table_column
|
15
|
-
else
|
16
|
-
html += "<fieldset class=\"#{attr.to_table_column}\">"
|
17
|
-
if attr.to_helper == :check_box
|
18
|
-
html += f.send attr.to_helper, attr.to_table_column
|
19
|
-
html += f.label attr.to_table_column
|
20
|
-
elsif attr.to_helper == :select
|
21
|
-
html += f.label attr.to_table_column
|
22
|
-
html += f.send attr.to_helper, attr.to_table_column, attr._options
|
23
|
-
else
|
24
|
-
html += f.label attr.to_table_column
|
25
|
-
html += f.send attr.to_helper, attr.to_table_column
|
26
|
-
end
|
27
|
-
html += "</fieldset>"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
html += '<div class="actions">'
|
31
|
-
puts "+++++++++++++++++++"
|
32
|
-
puts form._actions if block_given?
|
33
|
-
puts "+++++++++++++++++++"
|
34
|
-
html += f.submit
|
35
|
-
html += link_to "Cancel", root_path
|
36
|
-
html += '</div>'
|
37
|
-
raw html
|
38
|
-
end.to_s
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class ToyLocomotive::AutoViews::AutoForm
|
43
|
-
attr_accessor :_actions
|
44
|
-
|
45
|
-
def actions &blk
|
46
|
-
@_actions = blk
|
47
|
-
end
|
48
|
-
|
49
|
-
def _actions
|
50
|
-
@_actions.call.to_s
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
ActionView::Base.send :include, ToyLocomotive::AutoViews
|
1
|
+
module ToyLocomotive::AutoViews
|
2
|
+
def auto_form_for args
|
3
|
+
if block_given?
|
4
|
+
form = ToyLocomotive::AutoViews::AutoForm.new
|
5
|
+
yield(form)
|
6
|
+
end
|
7
|
+
form_for args do |f|
|
8
|
+
arg = args.class == Array ? args.last : args
|
9
|
+
klass = arg.class
|
10
|
+
html = ''
|
11
|
+
klass.attributes.each do |attr|
|
12
|
+
next if attr.skip_table_column?
|
13
|
+
if attr.to_helper == :hidden_field
|
14
|
+
html += f.send :hidden_field, attr.to_table_column
|
15
|
+
else
|
16
|
+
html += "<fieldset class=\"#{attr.to_table_column}\">"
|
17
|
+
if attr.to_helper == :check_box
|
18
|
+
html += f.send attr.to_helper, attr.to_table_column
|
19
|
+
html += f.label attr.to_table_column
|
20
|
+
elsif attr.to_helper == :select
|
21
|
+
html += f.label attr.to_table_column
|
22
|
+
html += f.send attr.to_helper, attr.to_table_column, attr._options
|
23
|
+
else
|
24
|
+
html += f.label attr.to_table_column
|
25
|
+
html += f.send attr.to_helper, attr.to_table_column
|
26
|
+
end
|
27
|
+
html += "</fieldset>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
html += '<div class="actions">'
|
31
|
+
puts "+++++++++++++++++++"
|
32
|
+
puts form._actions if block_given?
|
33
|
+
puts "+++++++++++++++++++"
|
34
|
+
html += f.submit
|
35
|
+
html += link_to "Cancel", root_path
|
36
|
+
html += '</div>'
|
37
|
+
raw html
|
38
|
+
end.to_s
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ToyLocomotive::AutoViews::AutoForm
|
43
|
+
attr_accessor :_actions
|
44
|
+
|
45
|
+
def actions &blk
|
46
|
+
@_actions = blk
|
47
|
+
end
|
48
|
+
|
49
|
+
def _actions
|
50
|
+
@_actions.call.to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
ActionView::Base.send :include, ToyLocomotive::AutoViews
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module ToyLocomotive::AutoViews
|
2
|
-
def auto_show_for args
|
3
|
-
arg = args.class == Array ? args.last : args
|
4
|
-
klass = arg.class
|
5
|
-
html = '<div class="attributes-show">'
|
6
|
-
klass.attributes.each do |attr|
|
7
|
-
html += '<div class="attr-set">'
|
8
|
-
html += '<div class="attr-label">'+attr.column.to_s+'</div>'
|
9
|
-
html += '<div class="attr-content">'+arg.send(attr.column).to_s+'</div>'
|
10
|
-
html += '</div>'
|
11
|
-
end
|
12
|
-
html += '<div class="actions">'
|
13
|
-
html += link_to "Editar", {:action => :edit}, {:class => :edit}
|
14
|
-
html += link_to "Deletar", {:action => :destroy}, {:class => :destroy, :method => :delete, :confirm => "Tem certeza?"}
|
15
|
-
html += '</div>'
|
16
|
-
html += '</div>'
|
17
|
-
raw html
|
18
|
-
end
|
19
|
-
end
|
1
|
+
module ToyLocomotive::AutoViews
|
2
|
+
def auto_show_for args
|
3
|
+
arg = args.class == Array ? args.last : args
|
4
|
+
klass = arg.class
|
5
|
+
html = '<div class="attributes-show">'
|
6
|
+
klass.attributes.each do |attr|
|
7
|
+
html += '<div class="attr-set">'
|
8
|
+
html += '<div class="attr-label">'+attr.column.to_s+'</div>'
|
9
|
+
html += '<div class="attr-content">'+arg.send(attr.column).to_s+'</div>'
|
10
|
+
html += '</div>'
|
11
|
+
end
|
12
|
+
html += '<div class="actions">'
|
13
|
+
html += link_to "Editar", {:action => :edit}, {:class => :edit}
|
14
|
+
html += link_to "Deletar", {:action => :destroy}, {:class => :destroy, :method => :delete, :confirm => "Tem certeza?"}
|
15
|
+
html += '</div>'
|
16
|
+
html += '</div>'
|
17
|
+
raw html
|
18
|
+
end
|
19
|
+
end
|
@@ -1,20 +1,20 @@
|
|
1
|
-
module ToyLocomotive
|
2
|
-
class Engine < Rails::Engine
|
3
|
-
initializer 'toy_locomotive.initialize', :after => :disable_dependency_loading do |app|
|
4
|
-
Dir["#{Rails.root}/app/models/*.rb"].each do |file|
|
5
|
-
require file
|
6
|
-
AttributeObserver.new file.split('/').last.split('.').first.classify.constantize
|
7
|
-
end
|
8
|
-
controllers = []
|
9
|
-
Dir["#{Rails.root}/app/controllers/*.rb"].each do |file|
|
10
|
-
require file
|
11
|
-
controllers << file.split('/').last.split('.').first.classify.constantize
|
12
|
-
controllers.last.append_filters!
|
13
|
-
end
|
14
|
-
Rails.application.class.routes.draw do
|
15
|
-
ToyLocomotive.routes.each {|route| match route[:path] => "#{route[:controller]}##{route[:action]}", as: route[:as], via: route[:method]}
|
16
|
-
controllers.each {|controller| controller.draws.each {|draw| send *draw}}
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
1
|
+
module ToyLocomotive
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
initializer 'toy_locomotive.initialize', :after => :disable_dependency_loading do |app|
|
4
|
+
Dir["#{Rails.root}/app/models/*.rb"].each do |file|
|
5
|
+
require file
|
6
|
+
AttributeObserver.new file.split('/').last.split('.').first.classify.constantize
|
7
|
+
end
|
8
|
+
controllers = []
|
9
|
+
Dir["#{Rails.root}/app/controllers/*.rb"].each do |file|
|
10
|
+
require file
|
11
|
+
controllers << file.split('/').last.split('.').first.classify.constantize
|
12
|
+
controllers.last.append_filters!
|
13
|
+
end
|
14
|
+
Rails.application.class.routes.draw do
|
15
|
+
ToyLocomotive.routes.each {|route| match route[:path] => "#{route[:controller]}##{route[:action]}", as: route[:as], via: route[:method]}
|
16
|
+
controllers.each {|controller| controller.draws.each {|draw| send *draw}}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,102 +1,102 @@
|
|
1
|
-
module ToyLocomotive::Resources::Controller
|
2
|
-
|
3
|
-
module ClassMethods
|
4
|
-
def extract_resources args
|
5
|
-
actions = {}
|
6
|
-
crud = [:index, :new, :create, :show, :edit, :update, :destroy]
|
7
|
-
if args.first == :all
|
8
|
-
actions[:crud]
|
9
|
-
hash = args.last
|
10
|
-
else
|
11
|
-
hash = args.first
|
12
|
-
end
|
13
|
-
actions[:crud] = hash[:only] if hash[:only]
|
14
|
-
actions[:crud] = crud - hash[:except] if hash[:except]
|
15
|
-
actions[:member] = hash[:member]
|
16
|
-
actions[:collection] = hash[:collection]
|
17
|
-
actions[:static] = hash[:static]
|
18
|
-
actions
|
19
|
-
end
|
20
|
-
|
21
|
-
def resources *args
|
22
|
-
res = extract_resources(args)
|
23
|
-
res[:crud].each{|action| send :"set_action_#{action}"} if res[:crud]
|
24
|
-
res[:member].each{|action| set_member_action(action)} if res[:member]
|
25
|
-
res[:collection].each{|action| set_collection_action(action)} if res[:collection]
|
26
|
-
res[:static].each{|action| set_static_action(action)} if res[:static]
|
27
|
-
end
|
28
|
-
|
29
|
-
def set_static_action action
|
30
|
-
get action.to_s do end
|
31
|
-
end
|
32
|
-
|
33
|
-
def set_member_action action
|
34
|
-
get action.to_s, on: 'member' do end
|
35
|
-
end
|
36
|
-
|
37
|
-
def set_collection_action action
|
38
|
-
get action.to_s, on: 'collection' do end
|
39
|
-
end
|
40
|
-
|
41
|
-
def set_action_new
|
42
|
-
get 'new' do
|
43
|
-
parent = extract_parent_vars.last
|
44
|
-
model = self.class.extract_model
|
45
|
-
instance_variable_set model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).new
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def set_action_index
|
50
|
-
get 'index' do
|
51
|
-
extract_parent_vars
|
52
|
-
extract_collection_var
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def set_action_show
|
57
|
-
get 'show' do
|
58
|
-
extract_parent_vars
|
59
|
-
extract_member_var
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def set_action_edit
|
64
|
-
get 'edit' do
|
65
|
-
extract_parent_vars
|
66
|
-
extract_member_var
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def set_action_create
|
71
|
-
post 'create' do
|
72
|
-
parent = (vars = extract_parent_vars).last
|
73
|
-
model = self.class.extract_model
|
74
|
-
member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_s.underscore.to_sym])
|
75
|
-
instance_variable_set model.to_member_var, member
|
76
|
-
vars = vars << member
|
77
|
-
return redirect_to vars, notice: 'Burrito was successfully created.' if member.save
|
78
|
-
render action: 'new'
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def set_action_update
|
83
|
-
put 'update' do
|
84
|
-
vars = extract_parent_vars
|
85
|
-
vars = vars << (member = extract_member_var)
|
86
|
-
return redirect_to vars, notice: 'Burrito was successfully updated.' if member.update_attributes(params[member.class.to_s.underscore.to_sym])
|
87
|
-
render action: 'edit'
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def set_action_destroy
|
92
|
-
delete 'destroy' do
|
93
|
-
vars = extract_parent_vars
|
94
|
-
extract_member_var.destroy
|
95
|
-
redirect_to action: :index, notice: 'Burrito was successfully deleted'
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
ActionController::Base.extend ToyLocomotive::Resources::Controller::ClassMethods
|
1
|
+
module ToyLocomotive::Resources::Controller
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
def extract_resources args
|
5
|
+
actions = {}
|
6
|
+
crud = [:index, :new, :create, :show, :edit, :update, :destroy]
|
7
|
+
if args.first == :all
|
8
|
+
actions[:crud] = crud
|
9
|
+
hash = args.last
|
10
|
+
else
|
11
|
+
hash = args.first
|
12
|
+
end
|
13
|
+
actions[:crud] = hash[:only] if hash[:only]
|
14
|
+
actions[:crud] = crud - hash[:except] if hash[:except]
|
15
|
+
actions[:member] = hash[:member]
|
16
|
+
actions[:collection] = hash[:collection]
|
17
|
+
actions[:static] = hash[:static]
|
18
|
+
actions
|
19
|
+
end
|
20
|
+
|
21
|
+
def resources *args
|
22
|
+
res = extract_resources(args)
|
23
|
+
res[:crud].each{|action| send :"set_action_#{action}"} if res[:crud]
|
24
|
+
res[:member].each{|action| set_member_action(action)} if res[:member]
|
25
|
+
res[:collection].each{|action| set_collection_action(action)} if res[:collection]
|
26
|
+
res[:static].each{|action| set_static_action(action)} if res[:static]
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_static_action action
|
30
|
+
get action.to_s do end
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_member_action action
|
34
|
+
get action.to_s, on: 'member' do end
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_collection_action action
|
38
|
+
get action.to_s, on: 'collection' do end
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_action_new
|
42
|
+
get 'new' do
|
43
|
+
parent = extract_parent_vars.last
|
44
|
+
model = self.class.extract_model
|
45
|
+
instance_variable_set model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).new
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_action_index
|
50
|
+
get 'index' do
|
51
|
+
extract_parent_vars
|
52
|
+
extract_collection_var
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_action_show
|
57
|
+
get 'show' do
|
58
|
+
extract_parent_vars
|
59
|
+
extract_member_var
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_action_edit
|
64
|
+
get 'edit' do
|
65
|
+
extract_parent_vars
|
66
|
+
extract_member_var
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def set_action_create
|
71
|
+
post 'create' do
|
72
|
+
parent = (vars = extract_parent_vars).last
|
73
|
+
model = self.class.extract_model
|
74
|
+
member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_s.underscore.to_sym])
|
75
|
+
instance_variable_set model.to_member_var, member
|
76
|
+
vars = vars << member
|
77
|
+
return redirect_to vars, notice: 'Burrito was successfully created.' if member.save
|
78
|
+
render action: 'new'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def set_action_update
|
83
|
+
put 'update' do
|
84
|
+
vars = extract_parent_vars
|
85
|
+
vars = vars << (member = extract_member_var)
|
86
|
+
return redirect_to vars, notice: 'Burrito was successfully updated.' if member.update_attributes(params[member.class.to_s.underscore.to_sym])
|
87
|
+
render action: 'edit'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_action_destroy
|
92
|
+
delete 'destroy' do
|
93
|
+
vars = extract_parent_vars
|
94
|
+
extract_member_var.destroy
|
95
|
+
redirect_to action: :index, notice: 'Burrito was successfully deleted'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
ActionController::Base.extend ToyLocomotive::Resources::Controller::ClassMethods
|