toy-locomotive 0.0.11 → 0.1.0

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.
@@ -2,14 +2,40 @@ module ToyLocomotive::Resources::Controller
2
2
 
3
3
  module ClassMethods
4
4
  def extract_resources args
5
- actions = [:index, :new, :create, :show, :edit, :update, :destroy]
6
- return actions if args == :all
7
- return args[:only] if args[:only]
8
- actions - args[:except]
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
9
35
  end
10
36
 
11
- def resources args
12
- extract_resources(args).each{|action| send :"set_action_#{action}"}
37
+ def set_collection_action action
38
+ get action.to_s, on: 'collection' do end
13
39
  end
14
40
 
15
41
  def set_action_new
@@ -17,6 +17,7 @@ module ToyLocomotive::Router::Controller
17
17
  end
18
18
 
19
19
  def route_as
20
+ return "" unless extract_model
20
21
  belongs_chain.map{|m| m.to_s.underscore }.join('_') << (belongs_chain.empty? ? extract_model.to_s.underscore : "_#{extract_model.to_s.underscore}")
21
22
  end
22
23
 
@@ -47,7 +48,9 @@ module ToyLocomotive::Router::Controller
47
48
  end
48
49
 
49
50
  def extract_path path, opts={}
51
+ return '/' if path == 'root'
50
52
  return path if path[0] == '/'
53
+ return "/#{path.to_s.gsub('_','-').parameterize}" unless extract_model
51
54
  return "#{route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
52
55
  return "#{route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
53
56
  return "#{route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
@@ -59,7 +62,9 @@ module ToyLocomotive::Router::Controller
59
62
  return route_as if path == 'show'
60
63
  return nil if method != 'get'
61
64
  action = extract_action path, opts
62
- path[0] == '/' ? action : "#{action}_#{route_as}"
65
+ as = path[0] == '/' ? action : "#{action}"
66
+ as << "_#{route_as}" if extract_model
67
+ as
63
68
  end
64
69
 
65
70
  def extract_action path, opts={}, method='get'
@@ -71,13 +76,13 @@ module ToyLocomotive::Router::Controller
71
76
  end
72
77
 
73
78
  def extract_model
74
- extract_controller.singularize.camelize.constantize
79
+ extract_controller.singularize.camelize.constantize rescue nil
75
80
  end
76
81
 
77
82
  def extract_filter action, path, opts, method
78
83
  return if path[0] == '/'
79
84
  return if %w(index show new edit destroy update create).include? path
80
- send :"add_#{opts[:on]}_filter", action
85
+ send :"add_#{opts[:on]}_filter", action if opts[:on]
81
86
  end
82
87
 
83
88
  def append_filters!
@@ -1,3 +1,3 @@
1
1
  module ToyLocomotive
2
- VERSION = "0.0.11"
2
+ VERSION = "0.1.0"
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.11
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails