mongoid-api-base 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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/LICENSE.md +21 -0
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/lib/mongoid-api-base.rb +5 -0
- data/lib/mongoid-api-base/concern.rb +212 -0
- data/lib/mongoid-api-base/version.rb +4 -0
- data/lib/renderers/csv.rb +33 -0
- data/mongoid-api-base.gemspec +26 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 23d0d53d8ecc497b0c2ba2f314e26a2e35685723
|
4
|
+
data.tar.gz: 2882a8c75adb182a8c9f773dfe2c2addda7bacad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c6c6cf1fa3960b32e46b6a1e5e87fcc8c8f2040349ca7f90ee79355db5bbce8fbc428d655b4b61d28af2294ab9d15619d07d8d19701023f7c96b1595c63c62f
|
7
|
+
data.tar.gz: d22e667ddc134a8a8259cae55df527fe61160c02080b94d71afcc9bbad732df1757f01c9ffbc37341d110b33f2c17e53478c6b5c230f8b5890199af46d57a765
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright © 2016 [Slate Studio](https://www.slatestudio.com)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# =============================================================================
|
2
|
+
# MongoidApiBase
|
3
|
+
# Rails concern to implement API controllers for Mongoid models.
|
4
|
+
# -----------------------------------------------------------------------------
|
5
|
+
# Usage Example:
|
6
|
+
#
|
7
|
+
# include MongoidApiBase
|
8
|
+
#
|
9
|
+
# defaults resource_class: Journal::JournalPost,
|
10
|
+
# per_page: 30
|
11
|
+
#
|
12
|
+
# has_scope :by_category
|
13
|
+
# has_scope :hidden, type: :boolean
|
14
|
+
# has_scope :not_hidden, type: :boolean
|
15
|
+
#
|
16
|
+
# json_config methods: %w(hex slug sorted_categories opengraph_image_url meta_title meta_description),
|
17
|
+
# index: {
|
18
|
+
# except: %w(body_markdown body_html),
|
19
|
+
# methods: %w(hex slug)
|
20
|
+
# }
|
21
|
+
# =============================================================================
|
22
|
+
module MongoidApiBase
|
23
|
+
extend ActiveSupport::Concern
|
24
|
+
included do
|
25
|
+
respond_to :json
|
26
|
+
respond_to :csv, only: %w(index)
|
27
|
+
|
28
|
+
class_attribute :resource_class
|
29
|
+
class_attribute :per_page
|
30
|
+
class_attribute :csv_options
|
31
|
+
class_attribute :json_options
|
32
|
+
|
33
|
+
def index
|
34
|
+
@chain = resource_class
|
35
|
+
|
36
|
+
apply_scopes_to_chain!
|
37
|
+
search_filter_chain!
|
38
|
+
paginate_chain!
|
39
|
+
set_total_count_header!
|
40
|
+
|
41
|
+
respond_to do |format|
|
42
|
+
format.json { render json: @chain.as_json(json_config(:index)) }
|
43
|
+
format.csv { render csv: @chain }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def show
|
48
|
+
object = find_object
|
49
|
+
object = get_object_version(object)
|
50
|
+
render json: object.as_json(json_config(:show))
|
51
|
+
end
|
52
|
+
|
53
|
+
def create
|
54
|
+
object = build_object
|
55
|
+
if object.save
|
56
|
+
render json: object.as_json(json_config(:create))
|
57
|
+
else
|
58
|
+
render json: object.errors, status: :unprocessable_entity
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def update
|
63
|
+
object = find_object
|
64
|
+
if object.update_attributes(resource_params)
|
65
|
+
render json: object.as_json(json_config(:update))
|
66
|
+
else
|
67
|
+
render json: object.errors, status: :unprocessable_entity
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def destroy
|
72
|
+
object = find_object
|
73
|
+
if object.destroy
|
74
|
+
render nothing: true
|
75
|
+
else
|
76
|
+
render json: object.errors, status: :unprocessable_entity
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def find_object
|
81
|
+
resource_class.find(params[:id])
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_object_version(object)
|
85
|
+
version = params[:version]
|
86
|
+
if version && object.respond_to?(:undo, true)
|
87
|
+
object.undo(nil, from: version.to_i + 1, to: resource.version)
|
88
|
+
object.version = version
|
89
|
+
end
|
90
|
+
object
|
91
|
+
end
|
92
|
+
|
93
|
+
def build_object
|
94
|
+
resource_class.new(resource_params)
|
95
|
+
end
|
96
|
+
|
97
|
+
def apply_scopes_to_chain!
|
98
|
+
if respond_to?(:apply_scopes, true)
|
99
|
+
@chain = apply_scopes(@chain)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def search_filter_chain!
|
104
|
+
query = params[:search]
|
105
|
+
if query
|
106
|
+
normalized_query = query.to_s.downcase
|
107
|
+
@chain = @chain.search(normalized_query, match: :all)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def page
|
112
|
+
@page ||= params[:page] || 1
|
113
|
+
end
|
114
|
+
|
115
|
+
def per_page
|
116
|
+
@per_page ||= params[:perPage] || self.class.per_page || 20
|
117
|
+
end
|
118
|
+
|
119
|
+
def paginate_chain!
|
120
|
+
@chain = begin
|
121
|
+
if page
|
122
|
+
@chain.page(page).per(per_page)
|
123
|
+
else
|
124
|
+
@chain.all
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def set_total_count_header!
|
130
|
+
if page
|
131
|
+
response.headers['X-Total-Count'] = @chain.total_count
|
132
|
+
else
|
133
|
+
response.headers['X-Total-Count'] = @chain.size
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def resource_class
|
138
|
+
@resource_class ||= self.class.resource_class
|
139
|
+
@resource_class ||= begin
|
140
|
+
namespaced_class = self.class.to_s.split("::").last.
|
141
|
+
sub(/Controller$/, "").singularize
|
142
|
+
namespaced_class.constantize
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def resource_request_name
|
147
|
+
resource_class.to_s.underscore.gsub("/", "_").gsub("::", "_")
|
148
|
+
end
|
149
|
+
|
150
|
+
def resource_params
|
151
|
+
permitted_params
|
152
|
+
end
|
153
|
+
|
154
|
+
def permitted_params
|
155
|
+
params.require(resource_request_name).permit!
|
156
|
+
end
|
157
|
+
|
158
|
+
def json_default_methods
|
159
|
+
[ :_list_item_title,
|
160
|
+
:_list_item_subtitle,
|
161
|
+
:_list_item_thumbnail,
|
162
|
+
:_document_versions ]
|
163
|
+
end
|
164
|
+
|
165
|
+
def json_default_options
|
166
|
+
@json_default_options ||= begin
|
167
|
+
default_options = {}
|
168
|
+
json_options = self.class.json_options || {}
|
169
|
+
[:only, :except, :methods].each do |name|
|
170
|
+
if json_options.key? name
|
171
|
+
default_options[name] = json_options[name]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
default_options[:methods] ||= []
|
175
|
+
default_options[:methods].concat(json_default_methods).uniq!
|
176
|
+
default_options
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def json_config(action)
|
181
|
+
json_options = self.class.json_options
|
182
|
+
if json_options && json_options.key?(action)
|
183
|
+
json_options = json_options[action]
|
184
|
+
json_options[:methods] ||= []
|
185
|
+
json_options[:methods].concat(json_default_methods).uniq!
|
186
|
+
json_options
|
187
|
+
else
|
188
|
+
json_default_options
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class_methods do
|
194
|
+
def defaults(options)
|
195
|
+
if options.key?(:resource_class)
|
196
|
+
self.resource_class = options[:resource_class]
|
197
|
+
end
|
198
|
+
|
199
|
+
if options.key?(:per_page)
|
200
|
+
self.resource_class = options[:per_page]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def csv_config(options={})
|
205
|
+
self.csv_options = options
|
206
|
+
end
|
207
|
+
|
208
|
+
def json_config(options)
|
209
|
+
self.json_options = options
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
ActionController::Renderers.add :csv do |objects, options|
|
2
|
+
return "" unless objects.first
|
3
|
+
|
4
|
+
object_klass = objects.first.class
|
5
|
+
|
6
|
+
return "" unless object_klass.respond_to? :fields
|
7
|
+
|
8
|
+
columns = object_klass.fields.keys
|
9
|
+
csv_options = self.class.csv_options || {}
|
10
|
+
|
11
|
+
if csv_options
|
12
|
+
if csv_options.key?(:only)
|
13
|
+
columns &= csv_options[:only].map(&:to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
if csv_options.key?(:except)
|
17
|
+
columns -= csv_options[:except].map(&:to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
if csv_options.key?(:methods)
|
21
|
+
columns += csv_options[:methods].map(&:to_s)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
str = CSV.generate do |row|
|
26
|
+
row << columns
|
27
|
+
objects.each do |obj|
|
28
|
+
row << columns.map { |c| obj.send(c) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
return str
|
33
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mongoid-api-base/version"
|
4
|
+
require "date"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "mongoid-api-base"
|
8
|
+
s.summary = "Rails concern to implement API controllers for Mongoid models."
|
9
|
+
s.homepage = "http://github.com/slate-studio/mongoid-api-base"
|
10
|
+
s.authors = [ "Alexander Kravets" ]
|
11
|
+
s.email = "alex@slatestudio.com"
|
12
|
+
s.date = Date.today.strftime("%Y-%m-%d")
|
13
|
+
s.extra_rdoc_files = %w[ README.md ]
|
14
|
+
s.license = "MIT"
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = [ "lib" ]
|
18
|
+
s.version = MongoidApiBase::VERSION
|
19
|
+
s.platform = Gem::Platform::RUBY
|
20
|
+
|
21
|
+
s.add_dependency "rails", MongoidApiBase::RAILS_VERSION
|
22
|
+
# Ruby ODM framework for MongoDB
|
23
|
+
s.add_dependency "mongoid", "~> 5.0"
|
24
|
+
# Clean, powerful, customizable and sophisticated paginator
|
25
|
+
s.add_dependency "kaminari"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-api-base
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Kravets
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.5.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.5.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongoid
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: kaminari
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email: alex@slatestudio.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.md
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/mongoid-api-base.rb
|
68
|
+
- lib/mongoid-api-base/concern.rb
|
69
|
+
- lib/mongoid-api-base/version.rb
|
70
|
+
- lib/renderers/csv.rb
|
71
|
+
- mongoid-api-base.gemspec
|
72
|
+
homepage: http://github.com/slate-studio/mongoid-api-base
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Rails concern to implement API controllers for Mongoid models.
|
96
|
+
test_files: []
|