Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Mongoid support #5

Closed
wants to merge 11 commits into from

2 participants

@ElvinEfendi

Added new branch called mongoid which will allow developers to use crudify with Mongoid ODM. Als instead of will_paginate now it uses kaminari.

@parndt
Collaborator

Why did you change the paginator as well?

@ElvinEfendi

Because will_paginate does not support mongoid, but kaminari does. It also supports MongoMapper(someone might need in the future). As to will_paginate mongoid support, someone has already asked pull request but they have not merged it yet, you can check discussion here: mislav/will_paginate#193

@parndt parndt commented on the diff
lib/crudify.rb
@@ -1,9 +1,3 @@
-require 'active_support'
-require 'active_record'
-require 'action_controller'
-require 'will_paginate'
-require 'meta_search'
@parndt Collaborator
parndt added a note

Why did you unrequire all of these?

I did not implement searching so no need for meta_search, neither used will_paginate(instead used kaminari). Sorry for the rest seems I did mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
@@ -41,6 +41,7 @@ def set_what
end
def index
+ find_all_#{plural_name}
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
@@ -131,22 +125,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])
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
@@ -113,8 +108,7 @@ def scope_with_options(scope)
# Finds one single result based on the id params.
def find_#{singular_name}
- set_instance(#{class_name}.find(params[:id],
- :include => #{options[:include].map(&:to_sym).inspect}))
+ set_instance(#{class_name}.includes(#{options[:include]}).find(params[:id]))
@parndt Collaborator
parndt added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
@@ -52,12 +53,6 @@ def new
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}].merge!({
- :position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
- })
- end
@parndt Collaborator
parndt added a note

Why did you remove this block of code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
((6 lines not shown))
- # 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
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt commented on the diff
lib/crudify/class_methods.rb
((7 lines not shown))
)
- # Methods that are only included when this controller is searchable.
@parndt Collaborator
parndt added a note

why the mass kill on searching?

I did not have time to port searching for mongoid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt
Collaborator

This seems to reduce functionality for the existing ORMs.

@parndt parndt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 15, 2012
  1. @ElvinEfendi
  2. @ElvinEfendi

    typo fix

    ElvinEfendi authored
  3. @ElvinEfendi

    will_pagiante to kaminari

    ElvinEfendi authored
  4. @ElvinEfendi

    type fix

    ElvinEfendi authored
  5. @ElvinEfendi

    positioning removed

    ElvinEfendi authored
  6. @ElvinEfendi
  7. @ElvinEfendi

    redirect to url

    ElvinEfendi authored
  8. @ElvinEfendi

    index action

    ElvinEfendi authored
Commits on Nov 16, 2012
  1. @ElvinEfendi
  2. @ElvinEfendi

    eager loading

    ElvinEfendi authored
  3. @ElvinEfendi

    typo fix

    ElvinEfendi authored
This page is out of date. Refresh to see the latest.
View
5 crudify.gemspec
@@ -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
View
8 lib/crudify.rb
@@ -1,9 +1,3 @@
-require 'active_support'
-require 'active_record'
-require 'action_controller'
-require 'will_paginate'
-require 'meta_search'
@parndt Collaborator
parndt added a note

Why did you unrequire all of these?

I did not implement searching so no need for meta_search, neither used will_paginate(instead used kaminari). Sorry for the rest seems I did mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
-
require 'crudify/hook_methods'
require 'crudify/class_methods'
require 'crudify/base'
@@ -13,4 +7,4 @@ class Engine < Rails::Engine
end
end
-ActionController::Base.send(:include, Crudify::Base)
+ActionController::Base.send(:include, Crudify::Base)
View
13 lib/crudify/base.rb
@@ -20,15 +20,14 @@ def self.default_options(model_name)
this_class = class_name.constantize
{
:title_attribute => "title",
- :use_class_name_as_title => false,
+ :use_class_name_as_title => true,
:paginate => true,
:sortable => true,
- :searchable => true,
:include => [],
- :order => ('position ASC' if this_class.table_exists? && this_class.column_names.include?('position')),
- :conditions => '',
- :search_conditions => '',
- :redirect_to_url => "admin_#{plural_name}_url",
+ :order => ({ position: :asc } if this_class.fields.keys.include?('position')),
+ :conditions => {},
+ :search_conditions => {},
+ :redirect_to_url => "#{plural_name}_url",
:log => Rails.env == 'development'
}
end
@@ -41,4 +40,4 @@ def self.append_features(base)
end
-end
+end
View
131 lib/crudify/class_methods.rb
@@ -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 %(
@@ -41,6 +41,7 @@ def set_what
end
def index
+ find_all_#{plural_name}
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
def show
@@ -52,12 +53,6 @@ def new
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}].merge!({
- :position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
- })
- end
@parndt Collaborator
parndt added a note

Why did you remove this block of code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@instance = @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
ok = before_create
return ok unless ok === true
@@ -113,8 +108,7 @@ def scope_with_options(scope)
# Finds one single result based on the id params.
def find_#{singular_name}
- set_instance(#{class_name}.find(params[:id],
- :include => #{options[:include].map(&:to_sym).inspect}))
+ set_instance(#{class_name}.includes(#{options[:include]}).find(params[:id]))
@parndt Collaborator
parndt added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
@@ -131,22 +125,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])
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- # 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
@parndt Collaborator
parndt added a note

indentation wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
@@ -154,108 +140,11 @@ def search_all_#{plural_name}
# from within the current controller.
protected :find_#{singular_name},
:find_all_#{plural_name},
- :paginate_all_#{plural_name},
- :search_all_#{plural_name}
+ :paginate_all_#{plural_name}
)
- # Methods that are only included when this controller is searchable.
@parndt Collaborator
parndt added a note

why the mass kill on searching?

I did not have time to port searching for mongoid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- 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
-end
+end
Something went wrong with that request. Please try again.