crud_concern 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/crud_concern.rb +78 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee489b33c373b384816ee3689697c8f69e99bdc9
4
+ data.tar.gz: 66bfb816b8b530ec2cf2b09853dd12f697923e3b
5
+ SHA512:
6
+ metadata.gz: 746834e6867c58c73eb343cb71b972837e058a7091e49d2a7e356892e2de476d2ac9c81733b810c041a2f9ae8144b4cfb8ff50268f6ce2a2375f79dd0656950b
7
+ data.tar.gz: ba48b5a231411fe0e0e71ac7f9f99f010cb0e597543973197e5229b5fd114723fd7cc6a2c853db21afcd4208f1ac15667f7a596c619f7ab304d268fcc8eaa85a
@@ -0,0 +1,78 @@
1
+ module CrudConcern
2
+ extend ActiveSupport::Concern
3
+
4
+ def index
5
+ render json: resources, include: inclusions, meta: meta
6
+ end
7
+
8
+ def show
9
+ render json: resource, include: inclusions
10
+ end
11
+
12
+ def create
13
+ if resource.valid?
14
+ yield(resource) if block_given?
15
+ resource.save!
16
+ render json: resource
17
+ else
18
+ render json: resource,
19
+ status: :unprocessable_entity, serializer: ErrorSerializer
20
+ end
21
+ end
22
+
23
+ def update
24
+ if resource.update_attributes(attributes)
25
+ render json: resource
26
+ else
27
+ render json: ErrorSerializer.serialize(resource.errors),
28
+ status: :unprocessable_entity
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ render json: resource.destroy
34
+ end
35
+
36
+ private
37
+
38
+ def resources
39
+ scope.order(params[:sort])
40
+ end
41
+
42
+ def resource
43
+ @resource ||= params[:id] ? klass.find(params[:id]) : klass.new(attributes)
44
+ end
45
+
46
+ def scope
47
+ klass.all
48
+ end
49
+
50
+ def meta
51
+ { total: scope.count }
52
+ end
53
+
54
+ def deserialize(hash)
55
+ ActiveModelSerializers::Deserialization.jsonapi_parse(hash)
56
+ end
57
+
58
+ def attributes
59
+ @attributes ||= deserialize(params)
60
+ end
61
+
62
+ def klass
63
+ self.class.name.gsub('Controller','').singularize.constantize
64
+ end
65
+
66
+ def inclusions
67
+ params[:include]
68
+ end
69
+
70
+ def limit
71
+ params[:limit].to_i || 50
72
+ end
73
+
74
+ def offset
75
+ page = params[:page] ? params[:page].to_i - 1 : 0
76
+ page * limit
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crud_concern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guirec Corbel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Just include the concern and you will be able to do CRUD operations
14
+ email: guirec.corbel@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/crud_concern.rb
20
+ homepage: https://github.com/GCorbel/crud_concern
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Simple concern to do CRUD operations
44
+ test_files: []