Skip to content

Commit

Permalink
Gave crud the notion of redirect paging.
Browse files Browse the repository at this point in the history
Fixes #1861
  • Loading branch information
parndt committed Jan 7, 2013
1 parent c401085 commit adc7e47
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 57 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Fixed page saving bug when default locale was set to something different than `en` or when it was changed after creating some pages. [#2088](https://github.com/refinery/refinerycms/pull/2088). [Philip Arndt](https://github.com/parndt)
* Moved page preview functionality to its own controller and made it so that you need to be logged in to use it. [#2089](https://github.com/refinery/refinerycms/pull/2089). [Philip Arndt](https://github.com/parndt)
* Fixed issue which allowed identical slugs to exist after page reordering. [#2092](https://github.com/refinery/refinerycms/pull/2092). [Philip Arndt](https://github.com/parndt)
* Gave crudify's actions the ability to redirect to a particular page of results when `params[:page]` is supplied to the action. [#1861](https://github.com/refinery/refinerycms/issues/1861). [Philip Arndt](https://github.com/parndt)

* [See full list](https://github.com/refinery/refinerycms/compare/2-0-stable...master)

Expand Down
114 changes: 59 additions & 55 deletions core/lib/refinery/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,49 +61,22 @@ def self.crudify_options
prepend_before_filter :find_#{singular_name},
:only => [:update, :destroy, :edit, :show]
prepend_before_filter :merge_position_into_params!, :only => :create
def new
@#{singular_name} = #{class_name}.new
end
def create
# if the position field exists, set this object as last object, given the conditions of this class.
if #{class_name}.column_names.include?("position") && params[:#{singular_name}][:position].nil?
params[:#{singular_name}].merge!({
:position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
})
end
if (@#{singular_name} = #{class_name}.create(params[:#{singular_name}])).valid?
flash.notice = t(
'refinery.crudify.created',
:what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(#{options[:redirect_to_url]})
else
unless request.xhr?
redirect_to :back
else
render :partial => '/refinery/message'
end
end
else
self.index
@dialog_successful = true
render :index
end
create_or_update_successful
else
unless request.xhr?
render :action => 'new'
else
render :partial => '/refinery/admin/error_messages', :locals => {
:object => @#{singular_name},
:include_object_name => true
}
end
create_or_update_unsuccessful 'new'
end
end
Expand All @@ -118,30 +91,9 @@ def update
:what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(#{options[:redirect_to_url]})
else
unless request.xhr?
redirect_to :back
else
render :partial => '/refinery/message'
end
end
else
self.index
@dialog_successful = true
render :index
end
create_or_update_successful
else
unless request.xhr?
render :action => 'edit'
else
render :partial => '/refinery/admin/error_messages', :locals => {
:object => @#{singular_name},
:include_object_name => true
}
end
create_or_update_unsuccessful 'edit'
end
end
Expand All @@ -152,7 +104,7 @@ def destroy
flash.notice = t('destroyed', :scope => 'refinery.crudify', :what => "'\#{title}'")
end
redirect_to #{options[:redirect_to_url]}
redirect_to redirect_url
end
# Finds one single result based on the id params.
Expand All @@ -170,6 +122,15 @@ def find_all_#{plural_name}(conditions = #{options[:conditions].inspect})
).order("#{options[:order]}")
end
def merge_position_into_params!
# if the position field exists, set this object as last object, given the conditions of this class.
if #{class_name}.column_names.include?("position") && params[:#{singular_name}][:position].nil?
params[:#{singular_name}].merge!({
:position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
})
end
end
# Paginate a set of @#{plural_name} that may/may not already exist.
def paginate_all_#{plural_name}
# If we have already found a set then we don't need to again
Expand All @@ -184,6 +145,16 @@ def paginate_all_#{plural_name}
@#{plural_name} = @#{plural_name}.paginate(:page => params[:page], :per_page => per_page)
end
def redirect_url
if params[:page].present?
page = params[:page].to_i rescue 1
page -= 1 while #{class_name}.paginate(:page => page).empty? && page > 1
#{options[:redirect_to_url]}(:page => page)
else
#{options[:redirect_to_url]}
end
end
# If the controller is being accessed via an ajax request
# then render only the collection of items.
def render_partial_response?
Expand All @@ -193,6 +164,35 @@ def render_partial_response?
end
end
def create_or_update_successful
if from_dialog?
self.index
@dialog_successful = true
render :index
else
if /true|on|1/ === params[:continue_editing]
if request.xhr?
render :partial => '/refinery/message'
else
redirect_to :back
end
else
redirect_back_or_default redirect_url
end
end
end
def create_or_update_unsuccessful(action)
if request.xhr?
render :partial => '/refinery/admin/error_messages', :locals => {
:object => @#{singular_name},
:include_object_name => true
}
else
render :action => action
end
end
# Returns a weighted set of results based on the query specified by the user.
def search_all_#{plural_name}
# First find normal results.
Expand All @@ -208,7 +208,11 @@ def search_all_#{plural_name}
:find_all_#{plural_name},
:paginate_all_#{plural_name},
:render_partial_response?,
:search_all_#{plural_name}
:search_all_#{plural_name},
:redirect_url,
:create_or_update_successful,
:create_or_update_unsuccessful,
:merge_position_into_params!
)

# Methods that are only included when this controller is searchable.
Expand Down
2 changes: 1 addition & 1 deletion images/app/views/refinery/admin/images/_grid_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
refinery.edit_admin_image_path(image),
:title => t('edit', :scope => 'refinery.admin.images') %>
<%= link_to refinery_icon_tag('delete.png'),
refinery.admin_image_path(image),
refinery.admin_image_path(image, params.slice(:page)),
:class => 'cancel confirm-delete',
:title => t('delete', :scope => 'refinery.admin.images'),
:data => {
Expand Down
3 changes: 2 additions & 1 deletion resources/lib/refinery/resources/dragonfly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def setup!

def configure!
app_resources = ::Dragonfly[:refinery_resources]
app_resources.configure_with(:rails) do |c|
app_resources.configure_with(:rails)
app_resources.configure do |c|
c.datastore.root_path = Refinery::Resources.datastore_root_path
c.url_format = Refinery::Resources.dragonfly_url_format
c.secret = Refinery::Resources.dragonfly_secret
Expand Down

0 comments on commit adc7e47

Please sign in to comment.