storytime-admin 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/{admin/application.js → storytime_admin.js} +1 -1
- data/app/assets/javascripts/storytime_admin/table_links.js.coffee +5 -0
- data/app/assets/stylesheets/storytime_admin.css.scss +2 -0
- data/app/assets/stylesheets/{admin → storytime_admin}/_content.css.scss +0 -0
- data/app/assets/stylesheets/{admin → storytime_admin}/_tables.css.scss +0 -0
- data/app/controllers/storytime_admin/application_controller.rb +38 -9
- data/app/controllers/storytime_admin/concerns/polymorphic_route_generation.rb +28 -0
- data/app/helpers/storytime_admin/application_helper.rb +12 -0
- data/app/views/storytime_admin/application/_form.html.haml +2 -2
- data/app/views/storytime_admin/application/_headers.html.haml +2 -1
- data/app/views/storytime_admin/application/_object.html.haml +2 -2
- data/app/views/storytime_admin/application/edit.html.haml +2 -2
- data/app/views/storytime_admin/application/index.html.haml +2 -2
- data/app/views/storytime_admin/application/new.html.haml +2 -2
- data/config/routes.rb +7 -1
- data/lib/storytime_admin.rb +8 -2
- data/lib/storytime_admin/engine.rb +6 -0
- data/lib/storytime_admin/version.rb +1 -1
- metadata +8 -6
- data/app/assets/stylesheets/admin.css.scss +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9efd1b6120c7321e55cd4420d1fa448a98143686
|
4
|
+
data.tar.gz: f7c29ae62e5a30b6e64993e1576ebe1d1498413f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f858a1b34617cd6366c31ad1fd33040f646293c9ad988e0a1f2050733b665c3ddd0721b71fd04e742b60a52927ebfc0f10829d6b14eef2b153f04dea0f4502f0
|
7
|
+
data.tar.gz: c36d792826ff52f7e47d8ea88c125cf5f5eaacfaab31b3d6c33af2e2232933552d0004f32357126dd79cee6dedb85a173fdf40a984e805116e29276ed6d13f9e
|
File without changes
|
File without changes
|
@@ -1,15 +1,19 @@
|
|
1
1
|
module StorytimeAdmin
|
2
2
|
class ApplicationController < StorytimeAdmin.base_controller.constantize
|
3
|
+
include StorytimeAdmin::Concerns::PolymorphicRouteGeneration
|
4
|
+
|
3
5
|
layout :set_layout
|
4
6
|
|
5
7
|
before_action :authenticate_user!
|
6
8
|
before_action :ensure_admin!
|
7
9
|
before_action :load_model, only: [:edit, :update, :destroy]
|
8
10
|
|
9
|
-
helper_method :model, :
|
11
|
+
helper_method :model, :model_display_name, :model_display_name_pluralized, :model_name,
|
12
|
+
:model_sym, :sort_column, :sort_direction, :admin_controller?, :headers,
|
13
|
+
:form_attributes, :index_attr, :current_user, :polymorphic_route_components
|
10
14
|
|
11
15
|
def index
|
12
|
-
@collection = model.all.page(params[:page]).per(20)
|
16
|
+
@collection = model.all.order("#{sort_column} #{sort_direction}").page(params[:page]).per(20)
|
13
17
|
end
|
14
18
|
|
15
19
|
def new
|
@@ -24,7 +28,7 @@ module StorytimeAdmin
|
|
24
28
|
@model.user = current_user if @model.respond_to? :user
|
25
29
|
|
26
30
|
if @model.save
|
27
|
-
redirect_to model, notice: "#{model_name} successfully created"
|
31
|
+
redirect_to polymorphic_route_components(model), notice: "#{model_name} successfully created"
|
28
32
|
else
|
29
33
|
render :new
|
30
34
|
end
|
@@ -32,7 +36,7 @@ module StorytimeAdmin
|
|
32
36
|
|
33
37
|
def update
|
34
38
|
if @model.update(permitted_params)
|
35
|
-
redirect_to model, notice: "#{model_name} successfully updated"
|
39
|
+
redirect_to polymorphic_route_components(model), notice: "#{model_name} successfully updated"
|
36
40
|
else
|
37
41
|
render :edit
|
38
42
|
end
|
@@ -40,20 +44,28 @@ module StorytimeAdmin
|
|
40
44
|
|
41
45
|
def destroy
|
42
46
|
@model.destroy
|
43
|
-
redirect_to model, notice: "#{model_name} successfully deleted"
|
47
|
+
redirect_to polymorphic_route_components(model), notice: "#{model_name} successfully deleted"
|
44
48
|
end
|
45
49
|
|
46
50
|
private
|
47
51
|
def permitted_params
|
48
|
-
params.require(
|
52
|
+
params.require(model.name.underscore.split("/").last).permit(permitted_attributes.map(&:to_sym))
|
49
53
|
end
|
50
54
|
|
51
55
|
def ensure_admin!
|
52
|
-
unless current_user &&
|
56
|
+
unless current_user && admin_check
|
53
57
|
redirect_to main_app.root_url, flash: { error: "Only admin users can access this page" }
|
54
58
|
end
|
55
59
|
end
|
56
60
|
|
61
|
+
def admin_check
|
62
|
+
if StorytimeAdmin.ensure_admin_scope.nil?
|
63
|
+
current_user.send(StorytimeAdmin.ensure_admin_method)
|
64
|
+
else
|
65
|
+
current_user.send(StorytimeAdmin.ensure_admin_method, send(StorytimeAdmin.ensure_admin_scope))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
57
69
|
def attributes
|
58
70
|
@attributes ||= model.columns.map(&:name)
|
59
71
|
end
|
@@ -92,12 +104,28 @@ module StorytimeAdmin
|
|
92
104
|
model_name.classify.constantize
|
93
105
|
end
|
94
106
|
|
107
|
+
def model_display_name
|
108
|
+
model_name.split("::").last
|
109
|
+
end
|
110
|
+
|
111
|
+
def model_display_name_pluralized
|
112
|
+
model_display_name.pluralize
|
113
|
+
end
|
114
|
+
|
95
115
|
def model_name
|
96
|
-
@model_name ||= self.class.name.
|
116
|
+
@model_name ||= self.class.name.gsub("Controller", "").gsub("StorytimeAdmin::", "").singularize
|
97
117
|
end
|
98
118
|
|
99
119
|
def model_sym
|
100
|
-
@model_sym ||= model.name.underscore.to_sym
|
120
|
+
@model_sym ||= model.name.underscore.gsub("/", "_").to_sym
|
121
|
+
end
|
122
|
+
|
123
|
+
def sort_column
|
124
|
+
model.column_names.include?(params[:sort]) ? params[:sort] : "#{model_name.underscore.pluralize}.id"
|
125
|
+
end
|
126
|
+
|
127
|
+
def sort_direction
|
128
|
+
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
|
101
129
|
end
|
102
130
|
|
103
131
|
def admin_controller?
|
@@ -113,5 +141,6 @@ module StorytimeAdmin
|
|
113
141
|
send("current_#{Admin.user_class_underscore_all}".to_sym)
|
114
142
|
end
|
115
143
|
end
|
144
|
+
|
116
145
|
end
|
117
146
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module StorytimeAdmin
|
2
|
+
module Concerns
|
3
|
+
module PolymorphicRouteGeneration
|
4
|
+
def self.included(base)
|
5
|
+
base.helper_method :polymorphic_route_components
|
6
|
+
end
|
7
|
+
|
8
|
+
def polymorphic_route_components(object_or_class_name)
|
9
|
+
object = nil
|
10
|
+
|
11
|
+
class_name = if object_or_class_name.is_a?(String)
|
12
|
+
object = object_or_class_name.constantize
|
13
|
+
object_or_class_name
|
14
|
+
elsif object_or_class_name.is_a?(Class)
|
15
|
+
object = object_or_class_name
|
16
|
+
object_or_class_name.name
|
17
|
+
else
|
18
|
+
object = object_or_class_name
|
19
|
+
object_or_class_name.class.name
|
20
|
+
end
|
21
|
+
|
22
|
+
pieces = class_name.split("::").map{|part| part.underscore.to_sym }
|
23
|
+
pieces.pop
|
24
|
+
pieces << object
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -24,5 +24,17 @@ module StorytimeAdmin
|
|
24
24
|
super
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
def sortable(column, title=nil)
|
29
|
+
title ||= column.titleize
|
30
|
+
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
|
31
|
+
direction_arrow = if column == sort_column
|
32
|
+
direction == "asc" ? icon("caret-up") : icon("caret-down")
|
33
|
+
else
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
link_to "#{title} #{direction_arrow}".html_safe, {:sort => column, :direction => direction}
|
38
|
+
end
|
27
39
|
end
|
28
40
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
= simple_form_for @model do |f|
|
1
|
+
= simple_form_for polymorphic_route_components(@model) do |f|
|
2
2
|
= f.error_notification
|
3
3
|
= render 'fields', f: f
|
4
4
|
= f.submit "Save", class: "btn btn-primary btn-outline"
|
5
|
-
= link_to "Cancel", model, class: "btn btn-link"
|
5
|
+
= link_to "Cancel", polymorphic_route_components(model), class: "btn btn-link"
|
@@ -1,3 +1,3 @@
|
|
1
|
-
%tr.table-row-link{ data: { url: url_for([:edit, object]) } }
|
1
|
+
%tr.table-row-link{ data: { url: url_for([:edit, *polymorphic_route_components(object)]) } }
|
2
2
|
= render 'row', object: object
|
3
|
-
%td.text-right= link_to icon('trash-o'), object, class: "btn btn-sm btn-outline btn-danger", id: "delete_#{model_sym.to_s}_#{object.id}", method: :delete, data: { confirm: "Are you sure you want to delete this #{
|
3
|
+
%td.text-right= link_to icon('trash-o'), polymorphic_route_components(object), class: "btn btn-sm btn-outline btn-danger", id: "delete_#{model_sym.to_s}_#{object.id}", method: :delete, data: { confirm: "Are you sure you want to delete this #{model_display_name}?" }
|
@@ -3,7 +3,7 @@
|
|
3
3
|
.scroll-panel-header
|
4
4
|
= link_to '#', class: "btn btn-default btn-icon pull-left visible-xs visible-sm", data: { toggle: "off-canvas", target: "#dashboard-nav" } do
|
5
5
|
%i.icon-st-icons-st-logo
|
6
|
-
= link_to "Cancel", model, class: "btn btn-default btn-sm pull-right"
|
7
|
-
%h3.scroll-panel-title Edit #{
|
6
|
+
= link_to "Cancel", polymorphic_route_components(model), class: "btn btn-default btn-sm pull-right"
|
7
|
+
%h3.scroll-panel-title Edit #{model_display_name}
|
8
8
|
.scroll-panel-body
|
9
9
|
= render "form"
|
@@ -3,8 +3,8 @@
|
|
3
3
|
.scroll-panel-header
|
4
4
|
= link_to '#', class: "btn btn-default btn-icon pull-left visible-xs visible-sm", data: { toggle: "off-canvas", target: "#dashboard-nav" } do
|
5
5
|
%i.icon-st-icons-st-logo
|
6
|
-
= link_to "New #{
|
7
|
-
%h3.scroll-panel-title=
|
6
|
+
= link_to "New #{model_display_name}", [:new, model_sym], class: "btn btn-sm btn-primary btn-outline pull-right"
|
7
|
+
%h3.scroll-panel-title= model_display_name_pluralized
|
8
8
|
.scroll-panel-body
|
9
9
|
= render 'table'
|
10
10
|
= paginate @collection, theme: 'twitter-bootstrap-3'
|
@@ -3,7 +3,7 @@
|
|
3
3
|
.scroll-panel-header
|
4
4
|
= link_to '#', class: "btn btn-default btn-icon pull-left visible-xs visible-sm", data: { toggle: "off-canvas", target: "#dashboard-nav" } do
|
5
5
|
%i.icon-st-icons-st-logo
|
6
|
-
= link_to "Cancel", model, class: "btn btn-default btn-sm pull-right"
|
7
|
-
%h3.scroll-panel-title New #{
|
6
|
+
= link_to "Cancel", polymorphic_route_components(model), class: "btn btn-default btn-sm pull-right"
|
7
|
+
%h3.scroll-panel-title New #{model_display_name}
|
8
8
|
.scroll-panel-body
|
9
9
|
= render "form"
|
data/config/routes.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
StorytimeAdmin::Engine.routes.draw do
|
2
2
|
StorytimeAdmin.models.each do |model|
|
3
|
-
|
3
|
+
if model.include?("::")
|
4
|
+
components = model.tableize.split("/")
|
5
|
+
resource_name = components.pop
|
6
|
+
namespace(components.join("/")){ resources resource_name }
|
7
|
+
else
|
8
|
+
resources model.tableize.to_sym
|
9
|
+
end
|
4
10
|
end
|
5
11
|
end
|
data/lib/storytime_admin.rb
CHANGED
@@ -2,10 +2,12 @@ require "storytime_admin/engine"
|
|
2
2
|
|
3
3
|
module StorytimeAdmin
|
4
4
|
class << self
|
5
|
-
mattr_accessor :nav_title, :models, :user_class, :layout, :base_controller
|
5
|
+
mattr_accessor :nav_title, :models, :user_class, :layout, :base_controller, :ensure_admin_method, :ensure_admin_scope
|
6
6
|
self.nav_title = "Admin"
|
7
7
|
self.base_controller = "::ApplicationController"
|
8
8
|
@@user_class = "User"
|
9
|
+
self.ensure_admin_method = "admin?"
|
10
|
+
self.ensure_admin_scope = nil
|
9
11
|
end
|
10
12
|
|
11
13
|
def self.configure(&block)
|
@@ -13,7 +15,11 @@ module StorytimeAdmin
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def self.models
|
16
|
-
|
18
|
+
@@models ||= begin
|
19
|
+
files = Dir.glob(Rails.root.join("app", "controllers", "storytime_admin", "**/*"))
|
20
|
+
files.select{|val| val.ends_with?("_controller.rb") }
|
21
|
+
.map{|controller| controller.split("storytime_admin").last.gsub("_controller.rb", "").sub("/", "").classify }
|
22
|
+
end
|
17
23
|
end
|
18
24
|
|
19
25
|
def self.user_class
|
@@ -11,5 +11,11 @@ require 'bootstrap-kaminari-views'
|
|
11
11
|
module StorytimeAdmin
|
12
12
|
class Engine < ::Rails::Engine
|
13
13
|
isolate_namespace StorytimeAdmin
|
14
|
+
|
15
|
+
initializer "storytime_admin.controller_helpers" do
|
16
|
+
ActiveSupport.on_load(:action_controller) do
|
17
|
+
include StorytimeAdmin::Concerns::PolymorphicRouteGeneration
|
18
|
+
end
|
19
|
+
end
|
14
20
|
end
|
15
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: storytime-admin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Van Der Beek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -272,11 +272,13 @@ files:
|
|
272
272
|
- MIT-LICENSE
|
273
273
|
- README.rdoc
|
274
274
|
- Rakefile
|
275
|
-
- app/assets/javascripts/
|
276
|
-
- app/assets/
|
277
|
-
- app/assets/stylesheets/
|
278
|
-
- app/assets/stylesheets/
|
275
|
+
- app/assets/javascripts/storytime_admin.js
|
276
|
+
- app/assets/javascripts/storytime_admin/table_links.js.coffee
|
277
|
+
- app/assets/stylesheets/storytime_admin.css.scss
|
278
|
+
- app/assets/stylesheets/storytime_admin/_content.css.scss
|
279
|
+
- app/assets/stylesheets/storytime_admin/_tables.css.scss
|
279
280
|
- app/controllers/storytime_admin/application_controller.rb
|
281
|
+
- app/controllers/storytime_admin/concerns/polymorphic_route_generation.rb
|
280
282
|
- app/helpers/storytime_admin/application_helper.rb
|
281
283
|
- app/views/storytime_admin/application/_fields.html.haml
|
282
284
|
- app/views/storytime_admin/application/_flashes.html.haml
|