storytime-admin 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4034243444693388c7c42a2c1d46ebfb8dcfadf2
4
- data.tar.gz: 8de839ef143272a9cd9ad59bb431779d3dab2cf3
3
+ metadata.gz: 9efd1b6120c7321e55cd4420d1fa448a98143686
4
+ data.tar.gz: f7c29ae62e5a30b6e64993e1576ebe1d1498413f
5
5
  SHA512:
6
- metadata.gz: e0d8878001d19386475cd970de8343eabb6e3c2b2b497601ae0e9ebb711416cd4e8d2dddc7838ad5fb646f5511222e1ecf7447d7123ac3e7b557f66c153ae185
7
- data.tar.gz: 658e530a9b76abe7350677c3e11a9477fa94cca222829e145268a6333f8ff8df73219ba1999152aa94d6643183678ea9765e13a7e482856d6055c6401e023935
6
+ metadata.gz: f858a1b34617cd6366c31ad1fd33040f646293c9ad988e0a1f2050733b665c3ddd0721b71fd04e742b60a52927ebfc0f10829d6b14eef2b153f04dea0f4502f0
7
+ data.tar.gz: c36d792826ff52f7e47d8ea88c125cf5f5eaacfaab31b3d6c33af2e2232933552d0004f32357126dd79cee6dedb85a173fdf40a984e805116e29276ed6d13f9e
@@ -1,3 +1,3 @@
1
1
  //= require jquery
2
2
  //= require jquery_ujs
3
- //= require_tree .
3
+ //= require_tree .
@@ -0,0 +1,5 @@
1
+ $ ->
2
+ $(".table-row-link").on "click", (e) ->
3
+ unless $(e.target).data("confirm")? || $(e.target).parent("a[data-confirm]").length > 0
4
+ url = $(@).data('url')
5
+ document.location.href = url
@@ -0,0 +1,2 @@
1
+ @import 'storytime_admin/content';
2
+ @import 'storytime_admin/tables';
@@ -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, :model_name, :model_sym, :admin_controller?, :headers, :form_attributes, :index_attr, :current_user
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(model_sym).permit(permitted_attributes.map(&:to_sym))
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 && current_user.admin?
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.split("::").last.gsub("Controller", "").singularize
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,2 +1,3 @@
1
1
  - index_attr.each do |attribute|
2
- %th= attribute.titleize
2
+ %th{:class => "sort_by"}
3
+ = sortable(attribute.parameterize)
@@ -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 #{model_name}?" }
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 #{model_name}
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 #{model_name}", [:new, model_sym], class: "btn btn-sm btn-primary btn-outline pull-right"
7
- %h3.scroll-panel-title= model_name.pluralize
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 #{model_name}
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
- resources model.tableize.to_sym
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
@@ -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
- Dir.glob(Rails.root.join("app", "controllers", "storytime_admin", "**/*")).map{|controller| controller.split("/").last.split("_controller")[0]}
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
@@ -1,3 +1,3 @@
1
1
  module StorytimeAdmin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.1.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-04-03 00:00:00.000000000 Z
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/admin/application.js
276
- - app/assets/stylesheets/admin.css.scss
277
- - app/assets/stylesheets/admin/_content.css.scss
278
- - app/assets/stylesheets/admin/_tables.css.scss
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
@@ -1,2 +0,0 @@
1
- @import 'admin/content';
2
- @import 'admin/tables';