toy-resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ Toy Resources
2
+ =========
3
+
4
+ Toy Resources is a component of [Toy Locomotive](https://github.com/Mortaro/Toy-Locomotive]), but can be used independently outside Toy Locomotive.
5
+
6
+
7
+ Getting Started
8
+ ---------------
9
+
10
+ All you need to start using Toy Resources is to add the following lines to the respective files:
11
+
12
+ # Gemfile
13
+ gem "toy-resources"
14
+
15
+ # config/routes.rb inside the 'draw block'
16
+ toy_resources
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ToyResources'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :toy-resources do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,15 @@
1
+ module ToyResources
2
+ mattr_accessor :routes
3
+ ToyResources.routes = {}
4
+ end
5
+
6
+ require 'action_controller'
7
+ require 'active_record'
8
+
9
+ require 'toy-resources/router'
10
+ require 'toy-resources/model'
11
+ require 'toy-resources/actions'
12
+ require 'toy-resources/methods'
13
+ require 'toy-resources/nested'
14
+ require 'toy-resources/plural'
15
+ require 'toy-resources/singular'
@@ -0,0 +1,39 @@
1
+ module ToyResources::Actions
2
+
3
+ def _index
4
+ extract_collection
5
+ end
6
+
7
+ def _show
8
+ extract_member
9
+ end
10
+
11
+ def _new
12
+ extract_build
13
+ end
14
+
15
+ def _create
16
+ build = extract_build
17
+ return redirect_to vars_list, notice: 'build created' if build.save
18
+ render :action => :new, error: 'failed to build'
19
+ end
20
+
21
+ def _edit
22
+ extract_member
23
+ end
24
+
25
+ def _update
26
+ member = extract_member
27
+ return redirect_to vars_list, notice: 'member updated' if member.update_attributes params[model.to_params_symbol]
28
+ render :action => :edit, error: 'failed to update'
29
+ end
30
+
31
+ def _destroy
32
+ member = extract_member
33
+ return redirect_to :action => :index, notice: 'member deleted' if member.destroy
34
+ render :action => :show, error: 'failed to delete'
35
+ end
36
+
37
+ end
38
+
39
+ ActionController::Base.send :include, ToyResources::Actions
@@ -0,0 +1,69 @@
1
+ module ToyResources::Methods
2
+
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def model
8
+ self.class.model
9
+ end
10
+
11
+ def vars_list
12
+ instance_variable_get model.member_var_name
13
+ end
14
+
15
+ def parents
16
+ []
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def resources *args
22
+ options = args.extract_options!
23
+ actions = {}
24
+ include(singular? ? ToyResources::Singular : ToyResources::Plural)
25
+ nested_to(options[:nested_to])
26
+ actions[:crud] = crud_actions if args.first == :all
27
+ actions[:crud] = crud_actions - options.delete(:except) if options[:except]
28
+ actions[:crud] = options.delete(:only) if options[:only]
29
+ define_actions self, actions, options
30
+ end
31
+
32
+ def model
33
+ to_s.gsub('Controller', '').underscore.singularize.camelize.constantize
34
+ end
35
+
36
+ protected
37
+
38
+ def forward_routes_for actions
39
+ node = ToyResources.routes
40
+ parents_symbols.each { |ps| node = (node[ps] ||= {}) }
41
+ (node[resource_symbol] ||= {}).merge! actions.merge(method: resource_type)
42
+ end
43
+
44
+ def define_actions controller, actions, options
45
+ actions[:crud].each { |action| alias_method action, :"_#{action}" } if actions[:crud]
46
+ actions[:member].each { |action| alias_method action, :_show } if actions[:member]
47
+ actions[:collection].each { |action| alias_method action, :_index } if actions[:collection]
48
+ forward_routes_for actions
49
+ end
50
+
51
+ def singular?
52
+ parents_symbols.any? && parents_symbols.last.to_s.classify.constantize.reflections[model.member_symbol]
53
+ end
54
+
55
+ def nested_to controllers
56
+ return unless controllers
57
+ include ToyResources::Nested
58
+ self.parents_symbols = controllers
59
+ end
60
+
61
+ def parents_symbols
62
+ []
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ ActionController::Base.send :include, ToyResources::Methods
@@ -0,0 +1,29 @@
1
+ module ToyResources::Model
2
+
3
+ def collection_symbol
4
+ to_s.pluralize.underscore.to_sym
5
+ end
6
+
7
+ def collection_var_name
8
+ "@#{collection_symbol.to_s}"
9
+ end
10
+
11
+ def member_symbol
12
+ to_s.underscore.to_sym
13
+ end
14
+
15
+ def member_var_name
16
+ "@#{member_symbol}"
17
+ end
18
+
19
+ def to_params_symbol
20
+ :"#{member_symbol}"
21
+ end
22
+
23
+ def to_params_id_symbol
24
+ :"#{member_symbol}_id"
25
+ end
26
+
27
+ end
28
+
29
+ ActiveRecord::Base.extend ToyResources::Model
@@ -0,0 +1,24 @@
1
+ module ToyResources::Nested
2
+
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def parents
8
+ self.class.parents_symbols.map do |symbol|
9
+ model = symbol.to_s.classify.constantize
10
+ member = model.find params[model.to_params_id_symbol]
11
+ instance_variable_set model.member_var_name, member
12
+ member
13
+ end
14
+ end
15
+
16
+ def vars_list
17
+ parents << instance_variable_get(model.member_var_name)
18
+ end
19
+
20
+ module ClassMethods
21
+ mattr_accessor :parents_symbols
22
+ end
23
+
24
+ end
@@ -0,0 +1,38 @@
1
+ module ToyResources::Plural
2
+
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def extract_collection
8
+ collection = (parents.any? ? parents.last.send(model.collection_symbol) : model).all
9
+ instance_variable_set model.collection_var_name, collection
10
+ end
11
+
12
+ def extract_member
13
+ member = (parents.any? ? parents.last.send(model.collection_symbol) : model).find params[:id]
14
+ instance_variable_set model.member_var_name, member
15
+ end
16
+
17
+ def extract_build
18
+ build = (parents.any? ? parents.last.send(model.collection_symbol) : model).new (params[model.to_params_symbol] || {})
19
+ instance_variable_set model.member_var_name, build
20
+ end
21
+
22
+ module ClassMethods
23
+
24
+ def crud_actions
25
+ [:index, :new, :create, :show, :edit, :update, :destroy]
26
+ end
27
+
28
+ def resource_symbol
29
+ model.collection_symbol
30
+ end
31
+
32
+ def resource_type
33
+ :resources
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,21 @@
1
+ module ToyResources::Router
2
+
3
+ def toy_resources
4
+ Dir["#{Rails.root}/app/controllers/*.rb"].each { |file| load file }
5
+ eval string_block_from ToyResources.routes
6
+ ToyResources.routes = {}
7
+ end
8
+
9
+ def string_block_from routes
10
+ routes.map do |model, options|
11
+ <<-BLOCK
12
+ #{options.delete(:method)}(:#{model}, only: #{options.delete(:crud).inspect}) do
13
+ #{string_block_from(options)}
14
+ end
15
+ BLOCK
16
+ end.join
17
+ end
18
+
19
+ end
20
+
21
+ ActionDispatch::Routing::Mapper.send :include, ToyResources::Router
@@ -0,0 +1,34 @@
1
+ module ToyResources::Singular
2
+
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def extract_member
8
+ member = parents.last.send model.member_symbol
9
+ instance_variable_set model.member_var_name, member
10
+ end
11
+
12
+ #to-do
13
+ def extract_build
14
+ # build = (parents.any? ? parents.last.send(model.collection_symbol) : model).new (params[model.to_params_symbol] || {})
15
+ # instance_variable_set model.member_var_name, build
16
+ end
17
+
18
+ module ClassMethods
19
+
20
+ def crud_actions
21
+ [:new, :create, :show, :edit, :update, :destroy]
22
+ end
23
+
24
+ def resource_symbol
25
+ model.member_symbol
26
+ end
27
+
28
+ def resource_type
29
+ :resource
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ module ToyResources
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy-resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Mortaro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &30458268 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *30458268
25
+ - !ruby/object:Gem::Dependency
26
+ name: toy-attributes
27
+ requirement: &30457836 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *30457836
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-rails
38
+ requirement: &30457452 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *30457452
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda
49
+ requirement: &30457056 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *30457056
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &30456744 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *30456744
69
+ description: Allows to declare resources and routes in a single line
70
+ email:
71
+ - christian.mortaro@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/tasks/toy-resources_tasks.rake
77
+ - lib/toy-resources/actions.rb
78
+ - lib/toy-resources/methods.rb
79
+ - lib/toy-resources/model.rb
80
+ - lib/toy-resources/nested.rb
81
+ - lib/toy-resources/plural.rb
82
+ - lib/toy-resources/router.rb
83
+ - lib/toy-resources/singular.rb
84
+ - lib/toy-resources/version.rb
85
+ - lib/toy-resources.rb
86
+ - MIT-LICENSE
87
+ - Rakefile
88
+ - README.md
89
+ homepage: https://github.com/Mortaro/Toy-Resources
90
+ licenses: []
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 144692211
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 144692211
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.16
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Handles the resources for Toy-Locomotive
119
+ test_files: []