toy-locomotive 0.2.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +0,0 @@
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 +0,0 @@
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 +0,0 @@
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 +0,0 @@
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 +0,0 @@
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,101 +0,0 @@
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
- args.shift
10
- end
11
- hash = args.last || {}
12
- actions[:crud] = hash[:only] if hash[:only]
13
- actions[:crud] = crud - hash[:except] if hash[:except]
14
- actions[:member] = hash[:member]
15
- actions[:collection] = hash[:collection]
16
- actions[:static] = hash[:static]
17
- actions
18
- end
19
-
20
- def resources *args
21
- res = extract_resources(args)
22
- res[:crud].each{|action| send :"set_action_#{action}"} if res[:crud]
23
- res[:member].each{|action| set_member_action(action)} if res[:member]
24
- res[:collection].each{|action| set_collection_action(action)} if res[:collection]
25
- res[:static].each{|action| set_static_action(action)} if res[:static]
26
- end
27
-
28
- def set_static_action action
29
- get action.to_s do end
30
- end
31
-
32
- def set_member_action action
33
- get action.to_s, on: 'member' do end
34
- end
35
-
36
- def set_collection_action action
37
- get action.to_s, on: 'collection' do end
38
- end
39
-
40
- def set_action_new
41
- get 'new' do
42
- parent = extract_parent_vars.last
43
- model = self.class.extract_model
44
- instance_variable_set model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).new
45
- end
46
- end
47
-
48
- def set_action_index
49
- get 'index' do
50
- extract_parent_vars
51
- extract_collection_var
52
- end
53
- end
54
-
55
- def set_action_show
56
- get 'show' do
57
- extract_parent_vars
58
- extract_member_var
59
- end
60
- end
61
-
62
- def set_action_edit
63
- get 'edit' do
64
- extract_parent_vars
65
- extract_member_var
66
- end
67
- end
68
-
69
- def set_action_create
70
- post 'create' do
71
- parent = (vars = extract_parent_vars).last
72
- model = self.class.extract_model
73
- member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_s.underscore.to_sym])
74
- instance_variable_set model.to_member_var, member
75
- vars = vars << member
76
- return redirect_to vars, notice: 'Burrito was successfully created.' if member.save
77
- render action: 'new'
78
- end
79
- end
80
-
81
- def set_action_update
82
- put 'update' do
83
- vars = extract_parent_vars
84
- vars = vars << (member = extract_member_var)
85
- return redirect_to vars, notice: 'Burrito was successfully updated.' if member.update_attributes(params[member.class.to_s.underscore.to_sym])
86
- render action: 'edit'
87
- end
88
- end
89
-
90
- def set_action_destroy
91
- delete 'destroy' do
92
- vars = extract_parent_vars
93
- extract_member_var.destroy
94
- redirect_to action: :index, notice: 'Burrito was successfully deleted'
95
- end
96
- end
97
-
98
- end
99
-
100
- end
101
- ActionController::Base.extend ToyLocomotive::Resources::Controller::ClassMethods
@@ -1,13 +0,0 @@
1
- module ToyLocomotive::Router::Block
2
-
3
- def draw *args
4
- (@_draws ||= []).push args
5
- end
6
-
7
- def draws
8
- @_draws || []
9
- end
10
-
11
- end
12
-
13
- ActionController::Base.extend ToyLocomotive::Router::Block
@@ -1,134 +0,0 @@
1
- module ToyLocomotive::Router::Controller
2
-
3
- module ClassMethods
4
-
5
- %w(get put post delete).each {|via| eval "def #{via} path, opts={}, &blk; match_action \"#{via}\", path, opts, blk; end"}
6
-
7
- def nested *args
8
- @_nested = *args
9
- end
10
-
11
- def belongs_chain
12
- (@_nested || []).map{|m| m.to_s.classify.constantize}
13
- end
14
-
15
- def route_chain
16
- belongs_chain.map{|m| m.to_route}.join
17
- end
18
-
19
- def route_as
20
- return "" unless extract_model
21
- belongs_chain.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? extract_model.to_s.underscore : "_#{extract_model.to_s.underscore}")
22
- end
23
-
24
- def match_action method, path, opts, blk
25
- action = extract_action path, opts, method
26
- extract_filter action, path, opts, method
27
- as = extract_as path, opts, method
28
- path = extract_path path, opts
29
- controller = extract_controller
30
- add_route method, action, path, as, controller
31
- define_method action, blk
32
- end
33
-
34
- def add_route method, action, path, as, controller
35
- ToyLocomotive.routes ||= []
36
- ToyLocomotive.routes << {method: method, action: action, path: path, controller: controller, as: as}
37
- puts ({method: method, action: action, path: path, controller: controller, as: as}.inspect)
38
- end
39
-
40
- def add_member_filter action
41
- @member_filters ||= []
42
- @member_filters << action.to_sym
43
- end
44
-
45
- def add_collection_filter action
46
- @collection_filters ||= []
47
- @collection_filters << action.to_sym
48
- end
49
-
50
- def extract_path path, opts={}
51
- return '/' if path == 'root'
52
- return path if path[0] == '/'
53
- return "/#{path.to_s.gsub('_','-').parameterize}" unless extract_model
54
- return "#{route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
55
- return "#{route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
56
- return "#{route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
57
- "#{route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
58
- end
59
-
60
- def extract_as path, opts={}, method='get'
61
- return route_as.pluralize if path == 'index'
62
- return route_as if path == 'show'
63
- return nil if method != 'get'
64
- action = extract_action path, opts
65
- as = path[0] == '/' ? action : "#{action}"
66
- as << "_#{route_as}" if extract_model
67
- as
68
- end
69
-
70
- def extract_action path, opts={}, method='get'
71
- (opts[:as] || (path == '/' ? 'root' : path)).parameterize.underscore
72
- end
73
-
74
- def extract_controller
75
- to_s.gsub('Controller', '').downcase
76
- end
77
-
78
- def extract_model
79
- extract_controller.singularize.camelize.constantize rescue nil
80
- end
81
-
82
- def extract_filter action, path, opts, method
83
- return if path[0] == '/'
84
- return if %w(index show new edit destroy update create).include? path
85
- send :"add_#{opts[:on]}_filter", action if opts[:on]
86
- end
87
-
88
- def append_filters!
89
- before_filter :extract_parent_vars, only: ((@member_filters || []) + (@collection_filters || []))
90
- before_filter :extract_member_var, only: (@member_filters || [])
91
- before_filter :extract_collection_var, only: (@collection_filters || [])
92
- end
93
-
94
- end
95
-
96
- module InstanceMethods
97
-
98
- def extract_parent_vars
99
- chain = self.class.belongs_chain.reverse.clone
100
- vars = []
101
- if chain.any?
102
- root = chain.pop
103
- parent = root.find(params[root.to_params])
104
- instance_variable_set root.to_member_var, parent
105
- vars << parent
106
- chain.each do |model|
107
- parent = parent.send(model.to_s.underscore.pluralize).find(params[model.to_params])
108
- instance_variable_set model.to_member_var, parent
109
- vars << parent
110
- end
111
- end
112
- vars
113
- end
114
-
115
- def extract_member_var
116
- parent = self.class.belongs_chain.pop#reverse.pop
117
- model = self.class.extract_model
118
- parent = parent ? instance_variable_get(parent.to_member_var) : nil
119
- instance_variable_set(model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
120
- end
121
-
122
- def extract_collection_var
123
- parent = self.class.belongs_chain.pop#reverse.pop
124
- model = self.class.extract_model
125
- parent = parent ? instance_variable_get(parent.to_member_var) : nil
126
- instance_variable_set(model.to_collection_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model.all))
127
- end
128
-
129
- end
130
-
131
- end
132
-
133
- ActionController::Base.extend ToyLocomotive::Router::Controller::ClassMethods
134
- ActionController::Base.send :include, ToyLocomotive::Router::Controller::InstanceMethods
@@ -1,40 +0,0 @@
1
- module ToyLocomotive::Router::Model
2
-
3
- #def belongs_to_route
4
- # reflections = reflect_on_all_associations(:belongs_to)
5
- # reflections.any? ? reflections.first.name.to_s.camelize.constantize : nil
6
- #end
7
-
8
- #def belongs_chain chain=[]
9
- # parent_route = (chain.last || self).belongs_to_route
10
- # parent_route ? belongs_chain(chain << parent_route) : chain
11
- #end
12
-
13
- #def route_chain
14
- # belongs_chain.reverse.map{|m| m.to_route}.join
15
- #end
16
-
17
- def to_route
18
- s = to_s.parameterize.downcase
19
- "/#{s.pluralize}/:#{s}_id"
20
- end
21
-
22
- def to_member_var
23
- "@#{to_s.underscore}"
24
- end
25
-
26
- def to_collection_var
27
- to_member_var.pluralize
28
- end
29
-
30
- def to_params
31
- :"#{to_s.underscore}_id"
32
- end
33
-
34
- #def to_as
35
- # belongs_chain.reverse!.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? to_s.underscore : "_#{to_s.underscore}")
36
- #end
37
-
38
- end
39
-
40
- ActiveRecord::Base.extend ToyLocomotive::Router::Model