On Sat, Jul 12, 2008 at 7:29 PM, Andrew Gehring
[email protected] wrote:
Andrew
I don’t know of any particular examples per se, but I took a peek at
the code for a few different extensions to get an idea of how it’s
done. I made a very simple extension to manage a website directory
(just one model, one controller, and two views–index and edit). The
key is using Radiant’s Admin::AbstractModelController.
I would post the extension on GitHub, but it seems to be down for
maintenance at the moment. Here’s the code for the whole extension:
./site_directory_extension.rb
class SiteDirectoryExtension < Radiant::Extension
version “1.0”
description “Adds a website directory to Radiant”
url “http://nams-cms.org”
define_routes do |map|
map.namespace(:admin) do |admin|
admin.resources :sites
end
map.with_options(:controller => ‘sites’) do |sites|
sites.search_sites ‘sites/search’, :action =>
‘search’
sites.sites ‘sites’, :action =>
‘index’
end
end
def activate
admin.tabs.add “Sites”, “/admin/sites”, :after => “Layouts”,
:visibility => [:all]
end
def deactivate
admin.tabs.remove “Sites”
end
end
./db/migrate/001_create_sites.rb
class CreateSites < ActiveRecord::Migration
def self.up
create_table :sites, :force => true do |t|
t.string :title, :url, :null => false
t.string :description
t.timestamps
end
# this part loads data from a fixture... like a bulk import of
defaults
yml =
YAML.load_file(“#{RAILS_ROOT}/vendor/extensions/site_directory/db/bootstrap/sites.yml”)
yml.keys.each do |key|
Site.find_or_create_by_title({
:title => yml[key][‘title’],
:url => yml[key][‘url’],
:description => yml[key][‘description’]
})
end
end
def self.down
drop_table :sites
end
end
./app/controllers/admin/sites_controller.rb
class Admin::SiteController < Admin::AbstractModelController
model_class Site
only_allow_access_to :new, :edit, :remove, :upload,
:when => [:developer, :admin],
:denied_url => { :controller => ‘sites’, :action => ‘index’ },
:denied_message => ‘You must have developer or administrator
privileges to perform this action.’
def index
# this depends on the will_paginate plugin… I included it as a
submodule using git submodule add
git://github.com/mislav/will_paginate.git
/vendor/plugins/will_paginate
# I executed that git command from the site_directory extension’s
root, so will_paginate is a submodule of the extension, not of the
radiant install
@sites = Site.paginate :all, :page => params[:page], :order =>
(params[:order_by].nil? ? ‘title ASC’ : params[:order_by])
end
end
./app/models/site.rb
class Site < ActiveRecord::Base
acts_as_ferret :fields => [:title, :url, :description]
end
./app/views/admin/sites/index.html.haml
- content_for ‘page_css’ do
:sass
.pagination
margin: 25px 0
// more CSS removed for brevity
%h1 Sites
= will_paginate @sites
%table#snippets.index{:cellspacing=>“0”, :border=>“0”,
:cellpadding=>“0”}
%thead
%tr
%th.user= link_to_unless_current(‘Title’, site_index_url) {
link_to_unless_current(‘Title’, :order_by => ‘url DESC’) }
%th.user= link_to_unless_current(‘URL’, :order_by => ‘url ASC’)
{ link_to_unless_current(‘URL’, :order_by => ‘url DESC’) }
%th.modify Modify
%tbody
- @sites.each do |site|
%tr.node.level-1
%td.user= link_to site.title, site_edit_url(:id => site)
%td.user= link_to site.url, site_edit_url(:id => site)
%td.remove
= link_to image(‘remove’, :alt => ‘Delete Site’),
site_remove_url(:id => site)
= will_paginate @sites
%p
= link_to ‘New Site’, site_new_url
./app/views/sites/edit.html.erb
this is in ERB because I get a undefined method error when I try to
use HAML… don’t know why
<%= @site.new_record? ? 'New' : 'Editing' %> Site
<% form_for(@site, :url => admin_sites_path) do |f| %>
<%= label 'site', 'title' %>
|
<%= text_field 'site', 'title', :class => "textbox" %>
|
The title text will appear first as the blue link to the site
|
<%= label 'site', 'url' %>
|
<%= text_field 'site', 'url', :class => "textbox" %>
|
The url will appear in green at the bottom of the site's
listing
|
<%= label 'site', 'description' %>
|
<%= text_area 'site', 'description', :style => "width:
100%;", :rows => "3" %>
|
The description will appear in black between the title and
URL.
|
<%= updated_stamp @site %>
<%= save_model_button(@site) %>
<%= save_model_and_continue_editing_button(@site) %>
or
<%= link_to "Cancel", site_index_url %>
<% end %>
Hopefully that should be enough to get you started.
–
Tim