simpleadmin 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/app/controllers/simple_admin/resources_controller.rb +68 -70
- data/lib/simpleadmin/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e16106fffda69e8ce0a3647d28e1c761f2d0a1d5
|
4
|
+
data.tar.gz: d76c5254c70f3ecd386d0f3c82ddf393b1260d2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62def8a8e18efdfc72fde4d5e0d376226051adc7d50f51f58f37746f6b6ac4692e5a41807c83901216fb11d12bf82d91ab11a2d0c5cb62b1bb483a34445836b4
|
7
|
+
data.tar.gz: 1b0f40e9c6d143f4241958feb785108e66d1d48cf633f75dc31c0a95caa1bbf0106821ef34b79d3b35843b3d0c346b9f8ec7e251b14e19f8f555afe7577be30f
|
@@ -1,100 +1,98 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
model_fields = params[:model_fields]
|
1
|
+
module SimpleAdmin
|
2
|
+
class ResourcesController < BaseController
|
3
|
+
def index
|
4
|
+
model_klass = params[:model_klass_name].constantize
|
5
|
+
model_fields = params[:model_fields]
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
per_page = params[:per_page].to_i
|
8
|
+
page = params[:page].to_i if params[:page].present?
|
9
|
+
total = model_klass.count
|
11
10
|
|
12
|
-
|
11
|
+
resources = model_klass.limit(per_page)
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
if params[:query].present?
|
14
|
+
search_result = search(params[:query], model_klass, params[:model_attributes])
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
resources = resources.offset((per_page * page) - per_page) if page.present?
|
22
|
-
resources = resources.order(params[:sort][:column_name] => params[:sort][:order]) if params[:sort].present?
|
16
|
+
resources = search_result[:resources]
|
17
|
+
total = search_result[:total]
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
20
|
+
resources = resources.offset((per_page * page) - per_page) if page.present?
|
21
|
+
resources = resources.order(params[:sort][:column_name] => params[:sort][:order]) if params[:sort].present?
|
26
22
|
|
27
|
-
|
28
|
-
|
23
|
+
resources = resources.map do |resource|
|
24
|
+
_resource = {}
|
29
25
|
|
30
|
-
|
31
|
-
|
26
|
+
resource.attributes.each do |attribute_name, attribute_value|
|
27
|
+
next unless model_fields.include?(attribute_name)
|
32
28
|
|
33
|
-
_resource
|
29
|
+
_resource[attribute_name] = attribute_value
|
34
30
|
end
|
35
31
|
|
36
|
-
|
37
|
-
resources: resources,
|
38
|
-
total: total
|
39
|
-
}
|
32
|
+
_resource
|
40
33
|
end
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
35
|
+
render json: {
|
36
|
+
resources: resources,
|
37
|
+
total: total
|
38
|
+
}
|
39
|
+
end
|
45
40
|
|
46
|
-
|
41
|
+
def show
|
42
|
+
model_klass = params[:model_klass_name].constantize
|
43
|
+
model_fields = params[:model_fields]
|
47
44
|
|
48
|
-
|
49
|
-
end
|
45
|
+
resource = model_klass.find(params[:id]).attributes.slice(*model_fields)
|
50
46
|
|
51
|
-
|
52
|
-
|
47
|
+
render json: resource
|
48
|
+
end
|
53
49
|
|
54
|
-
|
50
|
+
def create
|
51
|
+
model_klass = params[:model_klass_name].constantize
|
55
52
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
resource = model_klass.new(resource_params)
|
54
|
+
|
55
|
+
if resource.save
|
56
|
+
render json: resource
|
57
|
+
else
|
58
|
+
render json: resource.errors
|
61
59
|
end
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
def update
|
63
|
+
model_klass = params[:model_klass_name].constantize
|
64
|
+
resource = model_klass.find(params[:id])
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
66
|
+
if resource.update(resource_params)
|
67
|
+
render json: resource
|
68
|
+
else
|
69
|
+
render json: resource.errors
|
72
70
|
end
|
71
|
+
end
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
|
73
|
+
def destroy
|
74
|
+
model_klass = params[:model_klass_name].constantize
|
75
|
+
resource = model_klass.find(params[:id])
|
77
76
|
|
78
|
-
|
79
|
-
|
77
|
+
resource.destroy
|
78
|
+
end
|
80
79
|
|
81
|
-
|
80
|
+
private
|
82
81
|
|
83
|
-
|
84
|
-
|
82
|
+
def search(search_query, model_klass, model_attributes)
|
83
|
+
return [] if model_attributes.empty?
|
85
84
|
|
86
|
-
|
87
|
-
|
85
|
+
query = model_attributes.map { |model_attribute| "#{model_attribute} LIKE ?" }.join(' OR ')
|
86
|
+
query_arguments = model_attributes.map { "%#{search_query}%" }
|
88
87
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
{
|
89
|
+
resources: model_klass.where(query, *query_arguments),
|
90
|
+
total: model_klass.where(query, *query_arguments).count
|
91
|
+
}
|
92
|
+
end
|
94
93
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
94
|
+
def resource_params
|
95
|
+
params.require(:resource).permit!
|
96
|
+
end
|
99
97
|
end
|
100
98
|
end
|
data/lib/simpleadmin/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simpleadmin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitriy Strukov
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
version: '0'
|
138
138
|
requirements: []
|
139
139
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.
|
140
|
+
rubygems_version: 2.6.11
|
141
141
|
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: simple-admin
|