toy-locomotive 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,10 @@ module ToyLocomotive::Attributes::Model
14
14
  toy_attributes.select{|a| a.parent == self}
15
15
  end
16
16
 
17
+ def use_toy_attributes?
18
+ true
19
+ end
20
+
17
21
  end
18
22
  end
19
23
  ActiveRecord::Base.extend ToyLocomotive::Attributes::Model::ClassMethods
@@ -4,6 +4,7 @@ module ToyLocomotive
4
4
  attr_accessor :attribute
5
5
 
6
6
  def initialize model
7
+ return unless model.respond_to?(:use_toy_attributes?) && model.use_toy_attributes?
7
8
  @model = model
8
9
  unless ActiveRecord::Base.connection.table_exists? @model.table_name
9
10
  Class.new(ActiveRecord::Migration).create_table(@model.table_name.to_sym) do |t|
@@ -13,6 +14,7 @@ module ToyLocomotive
13
14
  @model.attributes.each do |attribute|
14
15
  set_attribute(attribute)
15
16
  @model.attr_accessible attribute.column
17
+ @model.attr_accessible :"#{attribute.column}_id" if attribute._as == :belongs_to
16
18
  end
17
19
  @model.reset_column_information
18
20
  end
@@ -14,8 +14,8 @@ module ToyLocomotive::Resources::Controller
14
14
 
15
15
  def set_action_new
16
16
  get 'new' do
17
- extract_parent_vars
18
- parent = instance_variable_get (model = self.class.extract_model).belongs_chain.reverse.pop.to_member_var
17
+ parent = extract_parent_vars.last
18
+ model = self.class.extract_model
19
19
  instance_variable_set model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).new
20
20
  end
21
21
  end
@@ -43,8 +43,8 @@ module ToyLocomotive::Resources::Controller
43
43
 
44
44
  def set_action_create
45
45
  post 'create' do
46
- vars = extract_parent_vars
47
- parent = instance_variable_get (model = self.class.extract_model).belongs_chain.reverse.pop.to_member_var
46
+ parent = (vars = extract_parent_vars).last
47
+ model = self.class.extract_model
48
48
  member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_s.underscore.to_sym])
49
49
  instance_variable_set model.to_member_var, member
50
50
  vars = vars << member
@@ -4,6 +4,22 @@ module ToyLocomotive::Router::Controller
4
4
 
5
5
  %w(get put post delete).each {|via| eval "def #{via} path, opts={}, &blk; match_action \"#{via}\", path, opts, blk; end"}
6
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
+ belongs_chain.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? extract_model.to_s.underscore : "_#{extract_model.to_s.underscore}")
21
+ end
22
+
7
23
  def match_action method, path, opts, blk
8
24
  action = extract_action path, opts, method
9
25
  extract_filter action, path, opts, method
@@ -32,18 +48,18 @@ module ToyLocomotive::Router::Controller
32
48
 
33
49
  def extract_path path, opts={}
34
50
  return path if path[0] == '/'
35
- return "#{extract_model.route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
36
- return "#{extract_model.route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
37
- return "#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
38
- "#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
51
+ return "#{route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
52
+ return "#{route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
53
+ return "#{route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
54
+ "#{route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
39
55
  end
40
56
 
41
57
  def extract_as path, opts={}, method='get'
42
- return extract_model.to_as.pluralize if path == 'index'
43
- return extract_model.to_as if path == 'show'
58
+ return route_as.pluralize if path == 'index'
59
+ return route_as if path == 'show'
44
60
  return nil if method != 'get'
45
61
  action = extract_action path, opts
46
- path[0] == '/' ? action : "#{action}_#{extract_model.to_as}"
62
+ path[0] == '/' ? action : "#{action}_#{route_as}"
47
63
  end
48
64
 
49
65
  def extract_action path, opts={}, method='get'
@@ -75,14 +91,14 @@ module ToyLocomotive::Router::Controller
75
91
  module InstanceMethods
76
92
 
77
93
  def extract_parent_vars
78
- chain = self.class.extract_model.belongs_chain
94
+ chain = self.class.belongs_chain.clone
79
95
  vars = []
80
96
  if chain.any?
81
97
  root = chain.pop
82
98
  parent = root.find(params[root.to_params])
83
99
  instance_variable_set root.to_member_var, parent
84
100
  vars << parent
85
- chain.reverse!.each do |model|
101
+ chain.each do |model|
86
102
  parent = parent.send(model.to_s.underscore.pluralize).find(params[model.to_params])
87
103
  instance_variable_set model.to_member_var, parent
88
104
  vars << parent
@@ -92,13 +108,15 @@ module ToyLocomotive::Router::Controller
92
108
  end
93
109
 
94
110
  def extract_member_var
95
- parent = (model = self.class.extract_model).belongs_chain.reverse.pop
111
+ parent = self.class.belongs_chain.reverse.pop
112
+ model = self.class.extract_model
96
113
  parent = parent ? instance_variable_get(parent.to_member_var) : nil
97
114
  instance_variable_set(model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
98
115
  end
99
116
 
100
117
  def extract_collection_var
101
- parent = (model = self.class.extract_model).belongs_chain.reverse.pop
118
+ parent = self.class.belongs_chain.reverse.pop
119
+ model = self.class.extract_model
102
120
  parent = parent ? instance_variable_get(parent.to_member_var) : nil
103
121
  instance_variable_set(model.to_collection_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model.all))
104
122
  end
@@ -1,18 +1,18 @@
1
1
  module ToyLocomotive::Router::Model
2
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
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
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
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
12
 
13
- def route_chain
14
- belongs_chain.reverse.map{|m| m.to_route}.join
15
- end
13
+ #def route_chain
14
+ # belongs_chain.reverse.map{|m| m.to_route}.join
15
+ #end
16
16
 
17
17
  def to_route
18
18
  s = to_s.parameterize.downcase
@@ -31,9 +31,9 @@ module ToyLocomotive::Router::Model
31
31
  :"#{to_s.underscore}_id"
32
32
  end
33
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
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
37
 
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module ToyLocomotive
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toy-locomotive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-08 00:00:00.000000000 Z
12
+ date: 2012-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &26057412 !ruby/object:Gem::Requirement
16
+ requirement: &27219312 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *26057412
24
+ version_requirements: *27219312
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec-rails
27
- requirement: &26056980 !ruby/object:Gem::Requirement
27
+ requirement: &27218736 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *26056980
35
+ version_requirements: *27218736
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &26055636 !ruby/object:Gem::Requirement
38
+ requirement: &27218244 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *26055636
46
+ version_requirements: *27218244
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shoulda
49
- requirement: &26054808 !ruby/object:Gem::Requirement
49
+ requirement: &27217476 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *26054808
57
+ version_requirements: *27217476
58
58
  description: a Different aproach to Rails applications
59
59
  email:
60
60
  - mortaro@towsta.com