Skip to content

Commit

Permalink
refactored core crud library to make it much more easily understandab…
Browse files Browse the repository at this point in the history
…le and provide more options to control what's going on without overriding. Migrated some functionality over to make use of it and more will follow.
  • Loading branch information
parndt committed Jan 11, 2010
1 parent 7b8ea92 commit 9d6765a
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 75 deletions.
Expand Up @@ -2,4 +2,4 @@ class Admin::NewsItemsController < Admin::BaseController

crudify :news_item, :order => "created_at DESC"

end
end
4 changes: 4 additions & 0 deletions vendor/plugins/news/app/models/news_item.rb
Expand Up @@ -23,4 +23,8 @@ def content=(value)
self.body = value
end

def self.per_page
20
end

end
24 changes: 10 additions & 14 deletions vendor/plugins/news/app/views/admin/news_items/index.html.erb
Expand Up @@ -9,27 +9,23 @@
</ul>
</div>
<div id='records'>
<% if searching? %>
<h2>Search Results for "<%= params[:search] %>"</h2>
<% if @news_items.size > 0 %>
<%= render :partial => "news_item", :collection => @news_items %>
<% else %>
<p>Sorry, no results found.</p>
<% end %>
<%= "<h2>Search Results for \"#{params[:search]}\"</h2>" if searching? %>
<% unless @news_items.empty? %>
<%= will_paginate @news_items, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<ul>
<%= render :partial => "news_item", :collection => @news_items %>
</ul>
<%= will_paginate @news_items, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<% else %>
<% if @news_items.size > 0 %>
<%= will_paginate @news_items, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<ul>
<%= render :partial => "news_item", :collection => @news_items %>
</ul>
<%= will_paginate @news_items, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<% else %>
<% unless searching? %>
<p>
<strong>
There are no news items yet.
Click "Create News Item" to add your first news item.
</strong>
</p>
<% else %>
<p>Sorry, no results found.</p>
<% end %>
<% end %>
</div>
10 changes: 1 addition & 9 deletions vendor/plugins/pages/app/controllers/admin/pages_controller.rb
@@ -1,14 +1,6 @@
class Admin::PagesController < Admin::BaseController

crudify :page, :conditions => "parent_id IS NULL", :order => "position ASC", :include => [:parts, :slugs, :children, :images]

def index
if searching?
@pages = Page.find_with_index params[:search]
else
@pages = Page.find_all_by_parent_id(nil, :order => "position ASC")
end
end
crudify :page, :conditions => "parent_id IS NULL", :order => "position ASC", :include => [:parts, :slugs, :children], :paging => false

def new
@page = Page.new
Expand Down
5 changes: 3 additions & 2 deletions vendor/plugins/pages/app/models/page.rb
Expand Up @@ -26,7 +26,7 @@ def destroy
if self.deletable?
super
else
puts "This page is not deletable. Please use .destroy! if you really want it gone or first"
puts "This page is not deletable. Please use .destroy! if you really want it gone or first:"
puts "unset .link_url" unless self.link_url.blank?
puts "unset .menu_match" unless self.menu_match.blank?
puts "set .deletable to true" unless self.deletable
Expand Down Expand Up @@ -115,7 +115,8 @@ def all_page_part_content
end

def self.per_page(dialog = false)
size = (dialog ? 14 : 20)
2
#size = (dialog ? 14 : 20)
end

end
4 changes: 2 additions & 2 deletions vendor/plugins/pages/app/views/admin/pages/index.html.erb
Expand Up @@ -16,7 +16,7 @@
</div>
<div id='records' class='tree'>
<% if searching? %>
<% if @pages.size > 0 %>
<% unless @pages.empty? %>
<h2>Search Results for "<%= params[:search] %>"</h2>
<ul id='sortable_list'>
<%= render :partial => "sortable_list" %>
Expand All @@ -27,7 +27,7 @@
</p>
<% end %>
<% else %>
<% if @pages.size > 0 %>
<% unless @pages.empty? %>
<ul id='sortable_list'>
<%= render :partial => "sortable_list" %>
</ul>
Expand Down
@@ -1,5 +1,5 @@
<form method="GET" action="<%= url %>">
Search
<input id="search" name="search" size="35" type="search" value="<%= params[:search] %>" />
<%= submit_tag 'Go' %>
</form>
<%= submit_tag 'Go', :name => "" %>
</form>
104 changes: 78 additions & 26 deletions vendor/plugins/refinery/lib/crud.rb
Expand Up @@ -18,16 +18,23 @@ def self.append_features(base)
module ClassMethods

def crudify(model_name, new_options = {})
options = {:title_attribute => "title", :order => 'position ASC', :conditions => '', :sortable => true, :searchable => true}
options.merge!(new_options)
options = {
:title_attribute => "title",
:order => 'position ASC',
:conditions => '',
:sortable => true,
:searchable => true,
:include => [],
:paging => true,
:search_conditions => ''
}.merge!(new_options)

singular_name = model_name.to_s
class_name = singular_name.camelize
plural_name = singular_name.pluralize

module_eval %(
before_filter :find_#{singular_name}, :only => [:update, :destroy, :edit]
before_filter :find_all_#{plural_name}, :only => :reorder
def new
@#{singular_name} = #{class_name}.new
Expand Down Expand Up @@ -98,43 +105,88 @@ def destroy
end
def find_#{singular_name}
@#{singular_name} = #{class_name}.find(params[:id])
@#{singular_name} = #{class_name}.find(params[:id], :include => %w(#{options[:include].join(' ')}))
end
def find_all_#{plural_name}
@#{plural_name} = #{class_name}.find(:all, :order => "#{options[:order]}", :conditions => "#{options[:conditions]}")
@#{plural_name} = #{class_name}.find :all,
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def paginate_all_#{plural_name}
@#{plural_name} = #{class_name}.paginate :page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def search_all_#{plural_name}
@#{plural_name} = #{class_name}.find_with_index params[:search],
:order => "#{options[:order]}",
:conditions => "#{options[:search_conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def search_and_paginate_all_#{plural_name}
@#{plural_name} = #{class_name}.paginate_search params[:search],
:page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:search_conditions]}",
:include => %w(#{options[:include].join(' ')})
end
protected :find_#{singular_name}, :find_all_#{plural_name}
protected :find_#{singular_name}, :find_all_#{plural_name}, :paginate_all_#{plural_name}, :search_all_#{plural_name}, :search_and_paginate_all_#{plural_name}
)

if options[:searchable]
module_eval %(
def index
if searching?
@#{plural_name} = #{class_name}.paginate_search params[:search],
:page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}"
else
@#{plural_name} = #{class_name}.paginate :page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}"
if options[:paging]
module_eval %(
def index
if searching?
search_and_paginate_all_#{plural_name}
else
paginate_all_#{plural_name}
end
end
end
)
)
else
module_eval %(
def index
if searching?
search_all_#{plural_name}
else
find_all_#{plural_name}
end
end
)
end

else
module_eval %(
def index
@#{plural_name} = #{class_name}.paginate :page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}"
end
)
if options[:paging]
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
def update_positions
unless params[:tree] == "true"
params[:sortable_list].each do |i|
Expand Down
Expand Up @@ -59,4 +59,8 @@ def value=(new_value)
self[:value] = new_value
end

def self.per_page
10
end

end
Expand Up @@ -8,6 +8,6 @@
:class => "cancel", :method => :delete,
:title => 'Remove this setting forever' if refinery_setting.destroyable %>
</span>
<%=h refinery_setting.name.titleize %> <span class="preview">- <%=h truncate(refinery_setting.value.to_s, :length => 40) %></span>
<%=h refinery_setting.name.titleize %> <span class="preview">- <%=h truncate(refinery_setting.value.to_s, :length => 40) %></span>
</span>
</li>
Expand Up @@ -6,28 +6,22 @@
</ul>
</div>
<div id='records'>
<% if searching? %>
<% if @refinery_settings.size > 0 %>
<ul>
<%= render :partial => 'refinery_setting', :collection => @refinery_settings %>
</ul>
<% else %>
<p>Sorry, no results found</p>
<% end %>
<% unless @refinery_settings.empty? %>
<%= will_paginate @refinery_settings, :previous_label => '&laquo;', :next_label => '&raquo;' unless searching? %>
<ul>
<%= render :partial => 'refinery_setting', :collection => @refinery_settings %>
</ul>
<%= will_paginate @refinery_settings, :previous_label => '&laquo;', :next_label => '&raquo;' unless searching? %>
<% else %>
<% if @refinery_settings.size > 0 %>
<%= will_paginate @refinery_settings, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<ul>
<%= render :partial => 'refinery_setting', :collection => @refinery_settings %>
</ul>
<%= will_paginate @refinery_settings, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<% else %>
<p>
<p>
<% if searching? %>
Sorry, no results found
<% else %>
<strong>
There are no settings yet.
Click "Create New Setting" to add your first setting.
</strong>
</p>
<% end %>
<% end %>
</p>
<% end %>
</div>

0 comments on commit 9d6765a

Please sign in to comment.