Skip to content

Commit

Permalink
will_pagiante to kaminari
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvinEfendi committed Nov 15, 2012
1 parent 3c43b99 commit 77a0a05
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 115 deletions.
5 changes: 2 additions & 3 deletions crudify.gemspec
Expand Up @@ -19,12 +19,11 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency('rails', '>= 3.0.0')
s.add_dependency('will_paginate', '>= 2.3.15')
s.add_dependency('meta_search', '>= 1.0.1')
s.add_dependency('kaminari')

s.add_development_dependency('shoulda', '>= 2.11.3')
s.add_development_dependency('sqlite3-ruby', '>= 1.3.3')
s.add_development_dependency('capybara', '>= 0.4.1')
s.add_development_dependency('selenium-webdriver', '>= 0.1.3')

end
end
1 change: 0 additions & 1 deletion lib/crudify/base.rb
Expand Up @@ -23,7 +23,6 @@ def self.default_options(model_name)
:use_class_name_as_title => false,
:paginate => true,
:sortable => true,
:searchable => true,
:include => [],
:order => ({ position: :asc } if this_class.fields.keys.include?('position')),
:conditions => '',
Expand Down
117 changes: 6 additions & 111 deletions lib/crudify/class_methods.rb
Expand Up @@ -18,7 +18,7 @@ def crudify(model_name, options = {})
class_name = singular_name.camelize
plural_name = singular_name.pluralize

options[:paginate] = (options[:paginate] && eval(class_name).respond_to?(:paginate))
options[:paginate] = (options[:paginate] && eval(class_name).respond_to?(:page))

module_eval %(
Expand Down Expand Up @@ -131,22 +131,14 @@ def paginate_all_#{plural_name}
# If we have already found a set then we don't need to again
find_all_#{plural_name} if @#{plural_name}.nil?
paginate_options = {:page => params[:page]}
scope = @#{plural_name}.page(params[:page])
# Seems will_paginate doesn't always use the implicit method.
if #{class_name}.methods.map(&:to_sym).include?(:per_page)
paginate_options.update(:per_page => #{class_name}.per_page)
# if per page is default then set it
if scope.respond_to?(:per) && params[:per_page].present?
scope = scope.per(params[:per_page])
end
set_collection(@#{plural_name}.paginate(paginate_options), false)
end
# Returns results based on the query specified by the user.
def search_all_#{plural_name}
params[:search] ||= {}
params[:search][:meta_sort] ||= #{options[:order].to_s.downcase.gsub(' ', '.').inspect}
@search ||= find_all_#{plural_name}.search(params[:search])
set_collection(@search, false)
set_collection(scope, false)
end
Expand All @@ -155,105 +147,8 @@ def search_all_#{plural_name}
protected :find_#{singular_name},
:find_all_#{plural_name},
:paginate_all_#{plural_name},
:search_all_#{plural_name}
)

# Methods that are only included when this controller is searchable.
if options[:searchable]

module_eval %(
def searching?
params && !params[:search].nil?
end
)

if options[:paginate]
module_eval %(
def index
search_all_#{plural_name}
paginate_all_#{plural_name}
end
)
else
module_eval %(
def index
unless searching?
find_all_#{plural_name}
else
search_all_#{plural_name}
end
end
)
end
else
if options[:paginate]
module_eval %(
def index
paginate_all_#{plural_name}
end
)
else
module_eval %(
def index
find_all_#{plural_name}
end
)
end

end

if options[:sortable]
module_eval %(
def reorder
find_all_#{plural_name}
end
# Based upon http://github.com/matenia/jQuery-Awesome-Nested-Set-Drag-and-Drop
def update_positions
previous = nil
# The list doesn't come to us in the correct order. Frustration.
0.upto((newlist ||= params[:ul]).length - 1) do |index|
hash = newlist[index.to_s]
moved_item_id = hash['id'].split(/#{singular_name}\\_?/)
@current_#{singular_name} = #{class_name}.find_by_id(moved_item_id)
if @current_#{singular_name}.respond_to?(:move_to_root)
if previous.present?
@current_#{singular_name}.move_to_right_of(#{class_name}.find_by_id(previous))
else
@current_#{singular_name}.move_to_root
end
else
@current_#{singular_name}.update_attribute(:position, index)
end
if hash['children'].present?
update_child_positions(hash, @current_#{singular_name})
end
previous = moved_item_id
end
#{class_name}.rebuild! if #{class_name}.respond_to?(:rebuild!)
render :nothing => true
end
def update_child_positions(node, #{singular_name})
0.upto(node['children'].length - 1) do |child_index|
child = node['children'][child_index.to_s]
child_id = child['id'].split(/#{singular_name}\_?/)
child_#{singular_name} = #{class_name}.find_by_id(child_id)
child_#{singular_name}.move_to_child_of(#{singular_name})
if child['children'].present?
update_child_positions(child, child_#{singular_name})
end
end
end
)
end

end

end
Expand Down

0 comments on commit 77a0a05

Please sign in to comment.