godmin 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +12 -0
- data/README.md +58 -8
- data/Rakefile +45 -16
- data/app/views/godmin/resource/show.html.erb +1 -1
- data/app/views/layouts/godmin/_layout.html.erb +1 -0
- data/app/views/layouts/godmin/application.html.erb +7 -1
- data/config/locales/en.yml +1 -1
- data/config/locales/pt-BR.yml +44 -0
- data/config/locales/sv.yml +1 -1
- data/godmin.gemspec +2 -1
- data/lib/generators/godmin/authentication/authentication_generator.rb +1 -1
- data/lib/godmin/application_controller.rb +1 -1
- data/lib/godmin/generators/named_base.rb +8 -0
- data/lib/godmin/resolver.rb +44 -18
- data/lib/godmin/resources/resource_controller.rb +17 -30
- data/lib/godmin/resources/resource_controller/batch_actions.rb +49 -0
- data/lib/godmin/version.rb +1 -1
- data/template.rb +180 -18
- data/test/dummy/admin/app/assets/javascripts/admin/application.js +1 -0
- data/test/dummy/app/assets/javascripts/application.js +1 -0
- data/test/dummy/app/controllers/application_controller.rb +2 -2
- data/test/dummy/app/controllers/articles_controller.rb +10 -0
- data/test/dummy/app/services/article_service.rb +31 -0
- data/test/dummy/app/services/secret_article_service.rb +0 -7
- data/test/dummy/db/migrate/20150717121532_create_articles.rb +1 -1
- data/test/dummy/db/schema.rb +3 -3
- data/test/integration/batch_actions_test.rb +51 -0
- data/test/integration/filters_test.rb +21 -0
- data/test/integration/scopes_test.rb +23 -0
- data/test/lib/godmin/resolver_test.rb +22 -8
- data/test/test_helper.rb +19 -3
- metadata +25 -3
@@ -1,12 +1,15 @@
|
|
1
1
|
require "godmin/helpers/batch_actions"
|
2
2
|
require "godmin/helpers/filters"
|
3
3
|
require "godmin/helpers/tables"
|
4
|
+
require "godmin/resources/resource_controller/batch_actions"
|
4
5
|
|
5
6
|
module Godmin
|
6
7
|
module Resources
|
7
8
|
module ResourceController
|
8
9
|
extend ActiveSupport::Concern
|
9
10
|
|
11
|
+
include BatchActions
|
12
|
+
|
10
13
|
included do
|
11
14
|
helper Godmin::Helpers::BatchActions
|
12
15
|
helper Godmin::Helpers::Filters
|
@@ -15,7 +18,7 @@ module Godmin
|
|
15
18
|
before_action :set_resource_service
|
16
19
|
before_action :set_resource_class
|
17
20
|
before_action :set_resources, only: :index
|
18
|
-
before_action :set_resource, only: [:show, :new, :edit, :create, :destroy]
|
21
|
+
before_action :set_resource, only: [:show, :new, :edit, :create, :update, :destroy]
|
19
22
|
end
|
20
23
|
|
21
24
|
def index
|
@@ -50,10 +53,6 @@ module Godmin
|
|
50
53
|
end
|
51
54
|
|
52
55
|
def update
|
53
|
-
return if perform_batch_action
|
54
|
-
|
55
|
-
set_resource
|
56
|
-
|
57
56
|
respond_to do |format|
|
58
57
|
if @resource_service.update_resource(@resource, resource_params)
|
59
58
|
format.html { redirect_to redirect_after_update, notice: redirect_flash_message }
|
@@ -128,7 +127,19 @@ module Godmin
|
|
128
127
|
end
|
129
128
|
|
130
129
|
def resource_params
|
131
|
-
params.require(resource_class.model_name.param_key.to_sym).permit(
|
130
|
+
params.require(@resource_class.model_name.param_key.to_sym).permit(resource_params_defaults)
|
131
|
+
end
|
132
|
+
|
133
|
+
def resource_params_defaults
|
134
|
+
@resource_service.attrs_for_form.map do |attribute|
|
135
|
+
association = @resource_class.reflect_on_association(attribute)
|
136
|
+
|
137
|
+
if association && association.macro == :belongs_to
|
138
|
+
association.foreign_key.to_sym
|
139
|
+
else
|
140
|
+
attribute
|
141
|
+
end
|
142
|
+
end
|
132
143
|
end
|
133
144
|
|
134
145
|
def redirect_after_create
|
@@ -150,30 +161,6 @@ module Godmin
|
|
150
161
|
def redirect_flash_message
|
151
162
|
translate_scoped("flash.#{action_name}", resource: @resource.class.model_name.human)
|
152
163
|
end
|
153
|
-
|
154
|
-
def perform_batch_action
|
155
|
-
return false unless params[:batch_action].present?
|
156
|
-
|
157
|
-
item_ids = params[:id].split(",").map(&:to_i)
|
158
|
-
records = @resource_class.find(item_ids)
|
159
|
-
if authorization_enabled?
|
160
|
-
records = records.select { |r| policy(r).send("batch_action_#{params[:batch_action]}?") }
|
161
|
-
end
|
162
|
-
|
163
|
-
if @resource_service.batch_action(params[:batch_action], records)
|
164
|
-
flash[:updated_ids] = item_ids
|
165
|
-
flash[:notice] = translate_scoped("flash.batch_action",
|
166
|
-
number_of_affected_records: records.length,
|
167
|
-
total_number_of_records: item_ids.length,
|
168
|
-
resource: @resource_class.model_name.human(count: item_ids.length))
|
169
|
-
|
170
|
-
if respond_to?("redirect_after_batch_action_#{params[:batch_action]}", true)
|
171
|
-
redirect_to send("redirect_after_batch_action_#{params[:batch_action]}") and return true
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
redirect_to :back and return true
|
176
|
-
end
|
177
164
|
end
|
178
165
|
end
|
179
166
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Godmin
|
2
|
+
module Resources
|
3
|
+
module ResourceController
|
4
|
+
module BatchActions
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
prepend_before_action :perform_batch_action, only: :update
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def perform_batch_action
|
14
|
+
return unless params[:batch_action].present?
|
15
|
+
|
16
|
+
set_resource_service
|
17
|
+
set_resource_class
|
18
|
+
|
19
|
+
if authorization_enabled?
|
20
|
+
authorize(batch_action_records, "batch_action_#{params[:batch_action]}?")
|
21
|
+
end
|
22
|
+
|
23
|
+
if @resource_service.batch_action(params[:batch_action], batch_action_records)
|
24
|
+
flash[:notice] = translate_scoped(
|
25
|
+
"flash.batch_action", number_of_records: batch_action_ids.length,
|
26
|
+
resource: @resource_class.model_name.human(count: batch_action_ids.length)
|
27
|
+
)
|
28
|
+
flash[:updated_ids] = batch_action_ids
|
29
|
+
|
30
|
+
if respond_to?("redirect_after_batch_action_#{params[:batch_action]}", true)
|
31
|
+
redirect_to send("redirect_after_batch_action_#{params[:batch_action]}")
|
32
|
+
return
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
redirect_to :back
|
37
|
+
end
|
38
|
+
|
39
|
+
def batch_action_ids
|
40
|
+
@_batch_action_ids ||= params[:id].split(",").map(&:to_i)
|
41
|
+
end
|
42
|
+
|
43
|
+
def batch_action_records
|
44
|
+
@_batch_action_records ||= @resource_class.where(id: batch_action_ids)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/godmin/version.rb
CHANGED
data/template.rb
CHANGED
@@ -7,9 +7,16 @@ def install_standalone
|
|
7
7
|
generate_model
|
8
8
|
generate("godmin:install")
|
9
9
|
generate("godmin:resource", "article")
|
10
|
+
generate("godmin:resource", "author")
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
modify_menu
|
13
|
+
modify_rakefile
|
14
|
+
modify_routes
|
15
|
+
modify_locales
|
16
|
+
modify_models
|
17
|
+
modify_author_service
|
18
|
+
modify_article_controller
|
19
|
+
modify_article_service
|
13
20
|
|
14
21
|
migrate_and_seed
|
15
22
|
end
|
@@ -23,7 +30,7 @@ def install_engine
|
|
23
30
|
|
24
31
|
inject_into_file "admin/admin.gemspec", before: /^end/ do
|
25
32
|
<<-END.strip_heredoc.indent(2)
|
26
|
-
s.add_dependency "godmin", "
|
33
|
+
s.add_dependency "godmin", "~> 1.0.0"
|
27
34
|
END
|
28
35
|
end
|
29
36
|
|
@@ -33,6 +40,7 @@ def install_engine
|
|
33
40
|
generate_model
|
34
41
|
run_ruby_script("admin/bin/rails g godmin:install")
|
35
42
|
run_ruby_script("admin/bin/rails g godmin:resource article")
|
43
|
+
run_ruby_script("admin/bin/rails g godmin:resource author")
|
36
44
|
|
37
45
|
inject_into_file "config/routes.rb", before: /^end/ do
|
38
46
|
<<-END.strip_heredoc.indent(2)
|
@@ -40,15 +48,22 @@ def install_engine
|
|
40
48
|
END
|
41
49
|
end
|
42
50
|
|
43
|
-
|
44
|
-
|
51
|
+
modify_menu("admin")
|
52
|
+
modify_rakefile
|
53
|
+
modify_routes("admin")
|
54
|
+
modify_locales
|
55
|
+
modify_models
|
56
|
+
modify_author_service("admin")
|
57
|
+
modify_article_controller("admin")
|
58
|
+
modify_article_service("admin")
|
45
59
|
|
46
60
|
migrate_and_seed
|
47
61
|
end
|
48
62
|
end
|
49
63
|
|
50
64
|
def generate_model
|
51
|
-
generate(:model, "
|
65
|
+
generate(:model, "author name:string")
|
66
|
+
generate(:model, "article title:string body:text author:references published:boolean published_at:datetime")
|
52
67
|
|
53
68
|
append_to_file "db/seeds.rb" do
|
54
69
|
<<-END.strip_heredoc
|
@@ -61,21 +76,121 @@ def generate_model
|
|
61
76
|
end
|
62
77
|
|
63
78
|
def author
|
64
|
-
|
79
|
+
Author.all.sample
|
65
80
|
end
|
66
81
|
|
67
82
|
def published
|
68
83
|
[true, true, false].sample
|
69
84
|
end
|
70
85
|
|
86
|
+
def published_at
|
87
|
+
Time.now - [0, 1, 2, 3, 4, 5].sample.days
|
88
|
+
end
|
89
|
+
|
90
|
+
["Lorem Ipsum", "Magna Aliqua", "Commodo Consequat"].each do |name|
|
91
|
+
Author.create name: name
|
92
|
+
end
|
93
|
+
|
71
94
|
35.times do |i|
|
72
|
-
Article.create! title: title, author: author, published: published
|
95
|
+
Article.create! title: title, author: author, published: published, published_at: published_at
|
73
96
|
end
|
74
97
|
END
|
75
98
|
end
|
76
99
|
end
|
77
100
|
|
78
|
-
def
|
101
|
+
def modify_menu(namespace = nil)
|
102
|
+
navigation_file =
|
103
|
+
if namespace
|
104
|
+
"admin/app/views/admin/shared/_navigation_aside.html.erb"
|
105
|
+
else
|
106
|
+
"app/views/shared/_navigation_aside.html.erb"
|
107
|
+
end
|
108
|
+
|
109
|
+
create_file navigation_file do
|
110
|
+
<<-END.strip_heredoc
|
111
|
+
<%= navbar_dropdown "Take me places" do %>
|
112
|
+
<%= navbar_item "Godmin on Github", "https://github.com/varvet/godmin" %>
|
113
|
+
<%= navbar_item "The source of this page!", "https://github.com/varvet/godmin-sandbox" %>
|
114
|
+
<%= navbar_item "The blog post", "https://www.varvet.se/blog/update/2015/11/13/introducing-godmin-1-0.html" %>
|
115
|
+
<%= navbar_divider %>
|
116
|
+
<%= navbar_item "Please retweet ;)", "https://twitter.com/varvet/status/665092299995676672" %>
|
117
|
+
<% end %>
|
118
|
+
END
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def modify_rakefile
|
123
|
+
append_to_file "RakeFile" do
|
124
|
+
<<-END.strip_heredoc
|
125
|
+
|
126
|
+
namespace :sandbox do
|
127
|
+
desc "Reseed the database"
|
128
|
+
task reseed: :environment do
|
129
|
+
Rake::Task["sandbox:reset"].invoke
|
130
|
+
Rake::Task["db:schema:load"].invoke
|
131
|
+
Rake::Task["db:seed"].invoke
|
132
|
+
end
|
133
|
+
desc "Reset the database"
|
134
|
+
task reset: :environment do
|
135
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
136
|
+
if table != "schema_migrations"
|
137
|
+
query = "DROP TABLE IF EXISTS \#{table} CASCADE;"
|
138
|
+
ActiveRecord::Base.connection.execute(query)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
END
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def modify_locales
|
148
|
+
append_to_file "config/locales/en.yml" do
|
149
|
+
<<-END.strip_heredoc.indent(2)
|
150
|
+
|
151
|
+
activerecord:
|
152
|
+
models:
|
153
|
+
article:
|
154
|
+
one: Article
|
155
|
+
other: Articles
|
156
|
+
author:
|
157
|
+
one: Author
|
158
|
+
other: Authors
|
159
|
+
END
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def modify_routes(namespace = nil)
|
164
|
+
routes_file =
|
165
|
+
if namespace
|
166
|
+
"admin/config/routes.rb"
|
167
|
+
else
|
168
|
+
"config/routes.rb"
|
169
|
+
end
|
170
|
+
|
171
|
+
gsub_file routes_file, "application#welcome", "articles#index"
|
172
|
+
end
|
173
|
+
|
174
|
+
def modify_models
|
175
|
+
inject_into_file "app/models/article.rb", before: "end" do
|
176
|
+
<<-END.strip_heredoc.indent(2)
|
177
|
+
|
178
|
+
def to_s
|
179
|
+
title
|
180
|
+
end
|
181
|
+
END
|
182
|
+
end
|
183
|
+
|
184
|
+
inject_into_file "app/models/author.rb", before: "end" do
|
185
|
+
<<-END.strip_heredoc.indent(2)
|
186
|
+
def to_s
|
187
|
+
name
|
188
|
+
end
|
189
|
+
END
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def modify_article_controller(namespace = nil)
|
79
194
|
articles_controller =
|
80
195
|
if namespace
|
81
196
|
"admin/app/controllers/admin/articles_controller.rb"
|
@@ -99,7 +214,7 @@ def modify_controller(namespace = nil)
|
|
99
214
|
end
|
100
215
|
end
|
101
216
|
|
102
|
-
def
|
217
|
+
def modify_article_service(namespace = nil)
|
103
218
|
article_service =
|
104
219
|
if namespace
|
105
220
|
"admin/app/services/admin/article_service.rb"
|
@@ -127,10 +242,10 @@ def modify_service(namespace = nil)
|
|
127
242
|
end
|
128
243
|
|
129
244
|
filter :title
|
130
|
-
filter :author
|
245
|
+
filter :author, as: :select, collection: -> { Author.all }, option_text: "name"
|
131
246
|
|
132
247
|
def filter_title(articles, value)
|
133
|
-
articles.where(title
|
248
|
+
articles.where("title LIKE ?", "%\#{value}%")
|
134
249
|
end
|
135
250
|
|
136
251
|
def filter_author(articles, value)
|
@@ -142,27 +257,74 @@ def modify_service(namespace = nil)
|
|
142
257
|
batch_action :destroy, confirm: true
|
143
258
|
|
144
259
|
def batch_action_unpublish(articles)
|
145
|
-
articles.
|
260
|
+
articles.update_all(published: false)
|
146
261
|
end
|
147
262
|
|
148
263
|
def batch_action_publish(articles)
|
149
|
-
articles.
|
264
|
+
articles.update_all(published: true)
|
150
265
|
end
|
151
266
|
|
152
267
|
def batch_action_destroy(articles)
|
153
|
-
articles.
|
268
|
+
articles.destroy_all
|
269
|
+
end
|
270
|
+
|
271
|
+
def per_page
|
272
|
+
15
|
154
273
|
end
|
155
274
|
END
|
156
275
|
end
|
157
276
|
end
|
158
277
|
|
278
|
+
def modify_author_service(namespace = nil)
|
279
|
+
author_service =
|
280
|
+
if namespace
|
281
|
+
"admin/app/services/admin/author_service.rb"
|
282
|
+
else
|
283
|
+
"app/services/author_service.rb"
|
284
|
+
end
|
285
|
+
|
286
|
+
gsub_file author_service, "attrs_for_index", "attrs_for_index :name"
|
287
|
+
gsub_file author_service, "attrs_for_show", "attrs_for_show :name"
|
288
|
+
gsub_file author_service, "attrs_for_form", "attrs_for_form :name"
|
289
|
+
|
290
|
+
inject_into_file author_service, after: "attrs_for_form :name \n" do
|
291
|
+
<<-END.strip_heredoc.indent(namespace ? 4 : 2)
|
292
|
+
attrs_for_export :id, :name
|
293
|
+
|
294
|
+
filter :name
|
295
|
+
|
296
|
+
def filter_name(authors, value)
|
297
|
+
authors.where("name LIKE ?", "%\#{value}%")
|
298
|
+
end
|
299
|
+
|
300
|
+
batch_action :destroy, confirm: true
|
301
|
+
|
302
|
+
def batch_action_destroy(authors)
|
303
|
+
authors.each { |a| a.destroy }
|
304
|
+
end
|
305
|
+
END
|
306
|
+
end
|
307
|
+
end
|
159
308
|
def migrate_and_seed
|
309
|
+
rake("db:drop")
|
310
|
+
rake("db:create")
|
160
311
|
rake("db:migrate")
|
161
312
|
rake("db:seed")
|
162
313
|
end
|
163
314
|
|
164
|
-
|
165
|
-
|
315
|
+
with_engine = "--with-engine"
|
316
|
+
without_engine = "--without-engine"
|
317
|
+
|
318
|
+
if ARGV.count > (ARGV - [with_engine, without_engine]).count
|
319
|
+
if ARGV.include? with_engine
|
320
|
+
install_engine
|
321
|
+
elsif ARGV.include? without_engine
|
322
|
+
install_standalone
|
323
|
+
end
|
166
324
|
else
|
167
|
-
|
325
|
+
if yes?("Place godmin in admin engine?")
|
326
|
+
install_engine
|
327
|
+
else
|
328
|
+
install_standalone
|
329
|
+
end
|
168
330
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class ApplicationController < ActionController::Base
|
2
|
+
include Godmin::ApplicationController
|
3
|
+
|
2
4
|
# Prevent CSRF attacks by raising an exception.
|
3
5
|
# For APIs, you may want to use :null_session instead.
|
4
6
|
protect_from_forgery with: :exception
|
5
|
-
|
6
|
-
include Godmin::ApplicationController
|
7
7
|
end
|