restaurant 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +2 -0
- data/Rakefile +5 -0
- data/app/assets/javascripts/restaurant/application.js +15 -0
- data/app/assets/stylesheets/restaurant/application.css +13 -0
- data/app/controllers/restaurant/application_controller.rb +4 -0
- data/app/helpers/restaurant/application_helper.rb +4 -0
- data/app/views/layouts/restaurant/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/lib/restaurant/accept_default.rb +7 -0
- data/lib/restaurant/config.rb +21 -0
- data/lib/restaurant/controller_helper.rb +11 -0
- data/lib/restaurant/controller_provider.rb +12 -0
- data/lib/restaurant/engine.rb +5 -0
- data/lib/restaurant/model_class_finder.rb +7 -0
- data/lib/restaurant/params_query_responder.rb +8 -0
- data/lib/restaurant/params_query_translator.rb +131 -0
- data/lib/restaurant/restful_actions.rb +15 -0
- data/lib/restaurant/router.rb +19 -0
- data/lib/restaurant/version.rb +3 -0
- data/lib/restaurant.rb +13 -0
- data/lib/tasks/restaurant_tasks.rake +4 -0
- metadata +164 -0
data/MIT-LICENSE
ADDED
@@ -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.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Restaurant</title>
|
5
|
+
<%= stylesheet_link_tag "restaurant/application", :media => "all" %>
|
6
|
+
<%= javascript_include_tag "restaurant/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Restaurant::Config
|
2
|
+
class << self
|
3
|
+
def roles
|
4
|
+
@roles ||= YAML.load_file(path)
|
5
|
+
end
|
6
|
+
|
7
|
+
def controllers
|
8
|
+
roles.inject([]) do |result, (role, controllers)|
|
9
|
+
result += controllers.keys
|
10
|
+
end.uniq
|
11
|
+
end
|
12
|
+
|
13
|
+
def path
|
14
|
+
Rails.root.join("config/roles.yml").tap do |path|
|
15
|
+
raise NoRolesError, "#{path} is not found" unless path.exist?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
NoRolesError = Class.new(StandardError)
|
21
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Restaurant::ControllerHelper
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include Restaurant::AcceptDefault
|
6
|
+
include Restaurant::ControllerProvider
|
7
|
+
include Restaurant::ModelClassFinder
|
8
|
+
include Restaurant::RestfulActions
|
9
|
+
self.responder = Restaurant::ParamsQueryResponder
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Restaurant::ControllerProvider
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
Restaurant::Config.controllers.each do |controller|
|
6
|
+
class_name = "#{controller.camelize.pluralize}Controller"
|
7
|
+
unless Object.const_defined?(class_name)
|
8
|
+
Object.const_set(class_name, Class.new(self))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class Restaurant::ParamsQueryResponder < ActionController::Responder
|
2
|
+
def initialize(controller, resources, options = {})
|
3
|
+
if resources.last.is_a? ActiveRecord::Relation
|
4
|
+
resources[-1] = Restaurant::ParamsQueryTranslator.translate(controller.params, resources.last)
|
5
|
+
end
|
6
|
+
super(controller, resources, options)
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
class Restaurant::ParamsQueryTranslator
|
2
|
+
def self.translate(*args)
|
3
|
+
new(*args).translate
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_reader :params, :resources
|
7
|
+
|
8
|
+
def initialize(params, resources)
|
9
|
+
@params = params
|
10
|
+
@resources = resources
|
11
|
+
end
|
12
|
+
|
13
|
+
def translate
|
14
|
+
filters.inject(resources) do |result, filter|
|
15
|
+
filter.call(result)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def filters
|
20
|
+
where_filters + order_filters
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def where_filters
|
26
|
+
where.map do |column, value|
|
27
|
+
WhereFilter.new(column, value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def order_filters
|
32
|
+
order.map do |column|
|
33
|
+
OrderFilter.new(column)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def where
|
38
|
+
params[:where] || []
|
39
|
+
end
|
40
|
+
|
41
|
+
def order
|
42
|
+
case params[:order]
|
43
|
+
when String
|
44
|
+
[params[:order]]
|
45
|
+
else
|
46
|
+
[]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class WhereFilter
|
51
|
+
attr_reader :column, :value
|
52
|
+
|
53
|
+
def initialize(column, value)
|
54
|
+
@column = column
|
55
|
+
@value = value
|
56
|
+
end
|
57
|
+
|
58
|
+
def call(resources)
|
59
|
+
if value.is_a? String
|
60
|
+
self.class.new(key => { "eq" => value }).call(*args)
|
61
|
+
else
|
62
|
+
sections.inject(resources) do |relation, section|
|
63
|
+
relation.where("#{column} #{section.operator}", *section.operand)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def sections
|
71
|
+
value.map do |operator, operand|
|
72
|
+
Section.new(operator, operand)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Section
|
77
|
+
attr_reader :raw_operator, :raw_operand
|
78
|
+
|
79
|
+
def initialize(raw_operator, raw_operand)
|
80
|
+
@raw_operator = raw_operator
|
81
|
+
@raw_operand = raw_operand
|
82
|
+
end
|
83
|
+
|
84
|
+
def operator
|
85
|
+
case raw_operator
|
86
|
+
when "eq"
|
87
|
+
"= ?"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def operand
|
92
|
+
case raw_operand
|
93
|
+
when true
|
94
|
+
[]
|
95
|
+
else
|
96
|
+
[raw_operand]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class OrderFilter
|
103
|
+
attr_reader :raw_column
|
104
|
+
|
105
|
+
def initialize(raw_column)
|
106
|
+
@raw_column = raw_column
|
107
|
+
end
|
108
|
+
|
109
|
+
def call(resources)
|
110
|
+
resources.order("#{column} #{order}")
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def column
|
116
|
+
raw_column.to_s.gsub(/^-/, "")
|
117
|
+
end
|
118
|
+
|
119
|
+
def order
|
120
|
+
if desc?
|
121
|
+
"DESC"
|
122
|
+
else
|
123
|
+
"ASC"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def desc?
|
128
|
+
/^-/ === raw_column.to_s
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Restaurant::RestfulActions
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include Restaurant::ModelClassFinder
|
6
|
+
end
|
7
|
+
|
8
|
+
def index
|
9
|
+
respond_with model_class.scoped
|
10
|
+
end
|
11
|
+
|
12
|
+
def show
|
13
|
+
respond_with model_class.find(params[:id])
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Restaurant::Router
|
2
|
+
def self.route(*args)
|
3
|
+
new(*args).route
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_reader :router
|
7
|
+
|
8
|
+
def initialize(router)
|
9
|
+
@router = router
|
10
|
+
end
|
11
|
+
|
12
|
+
def route
|
13
|
+
Restaurant::Config.roles.each do |role, controllers|
|
14
|
+
controllers.each do |controller_name, definitions|
|
15
|
+
router.resources controller_name, :only => definitions["actions"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/restaurant.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "restaurant/engine"
|
2
|
+
require "restaurant/accept_default"
|
3
|
+
require "restaurant/config"
|
4
|
+
require "restaurant/controller_helper"
|
5
|
+
require "restaurant/controller_provider"
|
6
|
+
require "restaurant/model_class_finder"
|
7
|
+
require "restaurant/params_query_responder"
|
8
|
+
require "restaurant/params_query_translator"
|
9
|
+
require "restaurant/restful_actions"
|
10
|
+
require "restaurant/router"
|
11
|
+
|
12
|
+
module Restaurant
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restaurant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryo Nakamura
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.13
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.13
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rack-accept-default
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec-rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.13.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.13.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec-json_matcher
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: pry-rails
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Restaurant serves RESTful API on Rails
|
111
|
+
email:
|
112
|
+
- r7kamura@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- app/assets/javascripts/restaurant/application.js
|
118
|
+
- app/assets/stylesheets/restaurant/application.css
|
119
|
+
- app/controllers/restaurant/application_controller.rb
|
120
|
+
- app/helpers/restaurant/application_helper.rb
|
121
|
+
- app/views/layouts/restaurant/application.html.erb
|
122
|
+
- config/routes.rb
|
123
|
+
- lib/restaurant/accept_default.rb
|
124
|
+
- lib/restaurant/config.rb
|
125
|
+
- lib/restaurant/controller_helper.rb
|
126
|
+
- lib/restaurant/controller_provider.rb
|
127
|
+
- lib/restaurant/engine.rb
|
128
|
+
- lib/restaurant/model_class_finder.rb
|
129
|
+
- lib/restaurant/params_query_responder.rb
|
130
|
+
- lib/restaurant/params_query_translator.rb
|
131
|
+
- lib/restaurant/restful_actions.rb
|
132
|
+
- lib/restaurant/router.rb
|
133
|
+
- lib/restaurant/version.rb
|
134
|
+
- lib/restaurant.rb
|
135
|
+
- lib/tasks/restaurant_tasks.rake
|
136
|
+
- MIT-LICENSE
|
137
|
+
- Rakefile
|
138
|
+
- README.md
|
139
|
+
homepage: https://github.com/r7kamura/restaurant
|
140
|
+
licenses: []
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ! '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 1.8.23
|
160
|
+
signing_key:
|
161
|
+
specification_version: 3
|
162
|
+
summary: Rails RESTful API server plugin
|
163
|
+
test_files: []
|
164
|
+
has_rdoc:
|