From 1be92e0db476fe0f2aabf4c13a0618fa2d8f992b Mon Sep 17 00:00:00 2001 From: did Date: Sat, 25 Jun 2011 17:01:11 -0700 Subject: [PATCH] roles fully implemented for the page / content sections + uniformize drag n drop in the contents section + add the missing selected value for the has_one field --- app/controllers/admin/contents_controller.rb | 13 +++++--- app/models/content_type.rb | 4 +-- app/views/admin/contents/_list.html.haml | 2 +- app/views/admin/contents/edit.html.haml | 8 +++-- app/views/admin/contents/index.html.haml | 12 ++++--- app/views/admin/contents/new.html.haml | 5 +-- .../custom_fields/types/_has_one.html.haml | 4 ++- app/views/admin/pages/_form.html.haml | 2 +- app/views/admin/pages/_page.html.haml | 2 +- .../admin/shared/_form_actions.html.haml | 2 +- doc/TODO | 6 ++-- public/javascripts/admin/contents.js | 26 ++++++++------- public/javascripts/admin/pages.js | 4 +-- .../stylesheets/admin/formtastic_changes.css | 32 ------------------- public/stylesheets/admin/layout.css | 27 ++++++++++++++++ 15 files changed, 79 insertions(+), 70 deletions(-) diff --git a/app/controllers/admin/contents_controller.rb b/app/controllers/admin/contents_controller.rb index acf18055..51e45ade 100644 --- a/app/controllers/admin/contents_controller.rb +++ b/app/controllers/admin/contents_controller.rb @@ -5,13 +5,14 @@ module Admin before_filter :set_content_type - respond_to :json, :only => :update + respond_to :json, :only => [:update, :sort] - # before_filter :authorize + skip_load_and_authorize_resource + + before_filter :authorize_content def index @contents = @content_type.list_or_group_contents - authorize! :index, ContentInstance end def create @@ -23,7 +24,7 @@ module Admin end def sort - @content_type.sort_contents!(params[:order]) + @content_type.sort_contents!(params[:children]) respond_with(@content_type, :location => admin_contents_url(@content_type.slug)) end @@ -42,5 +43,9 @@ module Admin set_content_type end + def authorize_content + authorize! params[:action].to_sym, ContentInstance + end + end end diff --git a/app/models/content_type.rb b/app/models/content_type.rb index 5579e84f..d56c02ba 100644 --- a/app/models/content_type.rb +++ b/app/models/content_type.rb @@ -107,8 +107,8 @@ class ContentType self.asc_order? ? list : list.reverse end - def sort_contents!(order) - order.split(',').each_with_index do |id, position| + def sort_contents!(ids) + ids.each_with_index do |id, position| self.contents.find(BSON::ObjectId(id))._position_in_list = position end self.save diff --git a/app/views/admin/contents/_list.html.haml b/app/views/admin/contents/_list.html.haml index 573ddd5c..497ede88 100644 --- a/app/views/admin/contents/_list.html.haml +++ b/app/views/admin/contents/_list.html.haml @@ -1,7 +1,7 @@ - if contents.empty? %p.no-items!= t('.no_items', :url => new_admin_content_url(@content_type.slug)) - else - %ul{ :id => 'contents-list', :class => "list #{'sortable' if @content_type.order_by == '_position_in_list'}" } + %ul{ :id => 'contents-list', :class => "list #{'sortable' if @content_type.order_by == '_position_in_list'}", :'data-url' => sort_admin_contents_path(@content_type.slug, :json) } - contents.each do |content| %li.content{ :id => "content-#{content._id}" } %em diff --git a/app/views/admin/contents/edit.html.haml b/app/views/admin/contents/edit.html.haml index 39a45487..ab197ca7 100644 --- a/app/views/admin/contents/edit.html.haml +++ b/app/views/admin/contents/edit.html.haml @@ -1,13 +1,15 @@ - title t('.title', :type => @content_type.name.capitalize) - + - content_for :submenu do = render 'admin/shared/menu/contents' - + - content_for :actions do = render 'admin/shared/actions/contents' - content_for :buttons do - = admin_button_tag t('admin.contents.index.edit'), edit_admin_content_type_url(@content_type), :class => 'edit' + - if can?(:manage, ContentType) + = admin_button_tag t('admin.contents.index.edit'), edit_admin_content_type_url(@content_type), :class => 'edit' + = admin_button_tag t('admin.contents.index.new'), new_admin_content_url(@content_type.slug), :class => 'new' %p= @content_type.description diff --git a/app/views/admin/contents/index.html.haml b/app/views/admin/contents/index.html.haml index abfcb52d..ce7f0ed6 100644 --- a/app/views/admin/contents/index.html.haml +++ b/app/views/admin/contents/index.html.haml @@ -10,7 +10,9 @@ = include_javascripts :contents - content_for :buttons do - = admin_button_tag :edit, edit_admin_content_type_url(@content_type), :class => 'edit' + - if can?(:manage, ContentType) + = admin_button_tag :edit, edit_admin_content_type_url(@content_type), :class => 'edit' + = admin_button_tag :new, new_admin_content_url(@content_type.slug), :class => 'new' - if @content_type.description.present? @@ -24,7 +26,7 @@ - else = render 'list', :contents => @contents -= form_tag sort_admin_contents_path(@content_type.slug), :method => :put, :class => 'formtastic' do - = hidden_field_tag :order - - = render 'admin/shared/form_actions', :delete_button => link_to(content_tag(:em, escape_once(' ')) + t('.destroy'), admin_content_type_url(@content_type), :confirm => t('admin.messages.confirm'), :method => :delete, :class => 'button small remove'), :button_label => :update \ No newline at end of file +- if can?(:manage, ContentType) + #local-actions-bottom-bar + %p.tleft + = link_to(content_tag(:em, escape_once(' ')) + t('.destroy'), admin_content_type_url(@content_type), :confirm => t('admin.messages.confirm'), :method => :delete, :class => 'button small remove') diff --git a/app/views/admin/contents/new.html.haml b/app/views/admin/contents/new.html.haml index 874d366b..dbcf6e2b 100644 --- a/app/views/admin/contents/new.html.haml +++ b/app/views/admin/contents/new.html.haml @@ -6,8 +6,9 @@ - content_for :actions do = render 'admin/shared/actions/contents' -- content_for :buttons do - = admin_button_tag t('admin.contents.index.edit'), edit_admin_content_type_url(@content_type), :class => 'edit' +- if can?(:manage, ContentType) + - content_for :buttons do + = admin_button_tag t('admin.contents.index.edit'), edit_admin_content_type_url(@content_type), :class => 'edit' %p= @content_type.description diff --git a/app/views/admin/custom_fields/types/_has_one.html.haml b/app/views/admin/custom_fields/types/_has_one.html.haml index 372e9942..d97c2415 100644 --- a/app/views/admin/custom_fields/types/_has_one.html.haml +++ b/app/views/admin/custom_fields/types/_has_one.html.haml @@ -1,4 +1,6 @@ - field.target.constantize.reload_parent! # to make sure all the contents from the parent are loaded -= form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field, form.object.send(field._alias.to_sym).try(:_id)) +- selected_id = form.object.send(field._alias.to_sym).try(:_id) + += form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'has_one' }, :as => :select, :collection => options_for_has_one(field, selected_id), :selected => selected_id diff --git a/app/views/admin/pages/_form.html.haml b/app/views/admin/pages/_form.html.haml index a26dd326..b457384b 100644 --- a/app/views/admin/pages/_form.html.haml +++ b/app/views/admin/pages/_form.html.haml @@ -11,7 +11,7 @@ - if not @page.index? and not @page.not_found? = f.input :parent_id, :as => :select, :collection => parent_pages_options, :include_blank => false - = f.input :slug, :required => false, :hint => @page.slug.blank? ? ' ' : page_url(@page), :input_html => { :data_url => get_path_admin_pages_url, :disabled => @page.index? || @page.not_found? }, :wrapper_html => { :style => "#{'display: none' if @page.templatized?}; height: 50px" } + = f.input :slug, :required => false, :hint => @page.slug.blank? ? ' ' : page_url(@page), :input_html => { :'data-url' => get_path_admin_pages_url, :disabled => @page.index? || @page.not_found? }, :wrapper_html => { :style => "#{'display: none' if @page.templatized?}; height: 50px" } = render 'editable_elements', :page => @page diff --git a/app/views/admin/pages/_page.html.haml b/app/views/admin/pages/_page.html.haml index 2b612881..b1f4f378 100644 --- a/app/views/admin/pages/_page.html.haml +++ b/app/views/admin/pages/_page.html.haml @@ -17,6 +17,6 @@ = link_to image_tag('admin/list/icons/trash.png'), admin_page_url(page), :class => 'remove', :confirm => t('admin.messages.confirm'), :method => :delete - if with_children - %ul{ :id => "folder-#{page._id}", :class => "folder depth-#{(page.depth || 0) + 1}", :data_url => sort_admin_page_url(page), :style => "display: #{cookies["folder-#{page._id}"] || 'block'}" } + %ul{ :id => "folder-#{page._id}", :class => "folder depth-#{(page.depth || 0) + 1}", :'data-url' => sort_admin_page_url(page), :style => "display: #{cookies["folder-#{page._id}"] || 'block'}" } = render children \ No newline at end of file diff --git a/app/views/admin/shared/_form_actions.html.haml b/app/views/admin/shared/_form_actions.html.haml index 1d936200..7109a251 100644 --- a/app/views/admin/shared/_form_actions.html.haml +++ b/app/views/admin/shared/_form_actions.html.haml @@ -1,4 +1,4 @@ -.actions +#local-actions-bottom-bar .span-12 %p - if defined?(back_url) diff --git a/doc/TODO b/doc/TODO index 0c03ed81..32789a63 100644 --- a/doc/TODO +++ b/doc/TODO @@ -35,9 +35,9 @@ x better hints: x model x ui - controllers / views: - - page - - asset - - content type + x page + x content / content type + x asset - site - account - snippet diff --git a/public/javascripts/admin/contents.js b/public/javascripts/admin/contents.js index 90e5cb1c..4ae128f5 100644 --- a/public/javascripts/admin/contents.js +++ b/public/javascripts/admin/contents.js @@ -1,18 +1,20 @@ $(document).ready(function() { - var updateContentsOrder = function() { - var lists = $('ul#contents-list.sortable'); - var ids = jQuery.map(lists, function(list) { - return(jQuery.map($(list).sortable('toArray'), function(el) { - return el.match(/content-(\w+)/)[1]; - }).join(',')); - }).join(','); - $('#order').val(ids || ''); - } + // sortable items $('ul#contents-list.sortable').sortable({ - handle: 'em', - items: 'li.content', - stop: function(event, ui) { updateContentsOrder(); } + 'handle': 'em', + 'items': 'li.content', + 'axis': 'y', + 'update': function(event, ui) { + var params = $(this).sortable('serialize', { 'key': 'children[]' }); + params += '&_method=put'; + params += '&' + $('meta[name=csrf-param]').attr('content') + '=' + $('meta[name=csrf-token]').attr('content'); + + $.post($(this).attr('data-url'), params, function(data) { + var error = typeof(data.error) != 'undefined'; + $.growl((error ? 'error' : 'success'), (error ? data.error : data.notice)); + }, 'json'); + } }); try { diff --git a/public/javascripts/admin/pages.js b/public/javascripts/admin/pages.js index c1c11bb5..3b9ccc85 100644 --- a/public/javascripts/admin/pages.js +++ b/public/javascripts/admin/pages.js @@ -26,7 +26,7 @@ $(document).ready(function() { params += '&_method=put'; params += '&' + $('meta[name=csrf-param]').attr('content') + '=' + $('meta[name=csrf-token]').attr('content'); - $.post($(this).attr('data_url'), params, function(data) { + $.post($(this).attr('data-url'), params, function(data) { var error = typeof(data.error) != 'undefined'; $.growl((error ? 'error' : 'success'), (error ? data.error : data.notice)); }, 'json'); @@ -82,7 +82,7 @@ $(document).ready(function() { var lookForSlugAndUrl = function() { params = 'parent_id=' + $('#page_parent_id').val() + "&slug=" + $('#page_slug').val(); - $.get($('#page_slug').attr('data_url'), params, function(data) { + $.get($('#page_slug').attr('data-url'), params, function(data) { $('#page_slug_input .inline-hints').html(data.url).effect('highlight'); }, 'json'); }; diff --git a/public/stylesheets/admin/formtastic_changes.css b/public/stylesheets/admin/formtastic_changes.css index 863be370..cbe2e367 100644 --- a/public/stylesheets/admin/formtastic_changes.css +++ b/public/stylesheets/admin/formtastic_changes.css @@ -165,38 +165,6 @@ form.formtastic fieldset ol .more { text-align: right; width: auto; margin: 10px form.formtastic fieldset ol .more a { text-decoration: none; color: #787A89; font-size: 0.7em; } form.formtastic fieldset ol .more a:hover { text-decoration: underline; } -/*form.formtastic hr { border-top: 2px solid #ccc; }*/ - -/*form.formtastic fieldset.buttons { padding-left: 28%; padding-bottom: 20px; }*/ - -form.formtastic div.actions { - position: relative; - top: 27px; - left: -15px; - width: 950px; - background: #8b8d9a; -} - -form.formtastic div.actions p { - padding: 15px; - margin: 0px; -} - -form.formtastic div.actions a { - color: #fff !important; - text-decoration: none; - font-size: 0.8em; - position: relative; - top: 4px; -} - -form.formtastic div.actions a.remove { color: #ff092c !important; } - -form.formtastic div.actions p a:hover { text-decoration: underline; } - -form.formtastic div.actions .last p { text-align: right; } - - /* ___ pages ___ */ form.formtastic fieldset ol li.path em { diff --git a/public/stylesheets/admin/layout.css b/public/stylesheets/admin/layout.css index 93eb469a..e15bec4d 100644 --- a/public/stylesheets/admin/layout.css +++ b/public/stylesheets/admin/layout.css @@ -151,6 +151,33 @@ body { #content #local-actions-bar a:hover { text-decoration: none; color: #333; } +#content #local-actions-bottom-bar { + position: relative; + top: 27px; + left: -15px; + width: 950px; + background: #8b8d9a; +} + +#content #local-actions-bottom-bar p { + padding: 15px; + margin: 0px; +} + +#content #local-actions-bottom-bar a { + color: #fff !important; + text-decoration: none; + font-size: 0.8em; + position: relative; + top: 4px; +} + +#content #local-actions-bottom-bar a.remove { color: #ff092c !important; } + +#content #local-actions-bottom-bar p a:hover { text-decoration: underline; } + +#content #local-actions-bottom-bar .last p { text-align: right; } + /* ___ footer ___ */ #footer {