Screencast: Administrationsfläche über Active Admin

Fast jede Applikation benötigt eine Administrationsfläche, mit der Werte angepasst und Texte verwaltet werden können. Active Admin bringt diese Möglichkeit der Verwaltung für Rails Applikationen mit und sieht obendrein auch nett aus. Ryan zeigt in diesem Screencast wie es installiert und benutzt wird.

 

Downloads in verschiedenen Formaten:

mp4
m4v
webm
ogg

 

Resourcen:

bash

[bash]
bundle rails g active_admin:install rake db:migrate rails g active_admin:resource product
[/bash]

Gemfile

[ruby]
gem ‚activeadmin‘
[/ruby]

app/admin/products.rb

[ruby]
ActiveAdmin.register Product do scope :unreleased index do column :name column :category column "Release Date", :released_at column :price, :sortable => :price do |product| div :class => "price" do number_to_currency product.price end end default_actions end end
[/ruby]

app/admin/dashboards.rb

[ruby]
ActiveAdmin::Dashboards.build do section "Recent Products" do table_for Product.order("released_at desc").limit(5) do column :name do |product| link_to product.name, [:admin, product] end column :released_at end strong { link_to "View All Products", admin_products_path } end end
[/ruby]

app/assets/stylesheets/application.css.scss

[css]
@import "products";
[/css]

config/initializers/active_admin.rb

[ruby]
config.site_title = "Ryan’s Store"
[/ruby]

Screencast: Suchen, Sortieren, Pagination mit AJAX

Viele Administrationsseiten bieten die Möglichkeite zu Suchen, Sortieren oder Daten seitenweise anzuzeigen. In dieser Woche zeigt Ryan wie diese Funktionen mit AJAX umgesetzt werden können.

 

Download:

Download(20.7 MB, 13:43)
Alternativer Download für iPod & Apple TV(19.9 MB, 13:43)

 

Resourcen:

 

Quellcode:

[bash]
rails g jquery:install
[/bash]

[ruby]
# Gemfile
gem ‚will_paginate‘, ‚3.0.pre2‘
gem ‚jquery-rails‘

# products_controller.rb
def index
@products = Product.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end

# models/product.rb
def self.search(search)
if search
where(’name LIKE ?‘, "%#{search}%")
else
scoped
end
end

# helpers/application_helper.rb
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
[/ruby]

[html]
<!– products/index.html.erb –>
<%= form_tag products_path, :method => ‚get‘, :id => "products_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="products"><%= render ‚products‘ %></div>
<% end %>

<!– products/_products.html.erb –>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= will_paginate @products %>

<!– products/index.js.erb –>
$("#products").html("<%= escape_javascript(render("products")) %>");
[/html]

Screencast: Declarative Authorization

Benutzer-Authorisierung wird in vielen, wenn nicht sogar in den meisten, Applikationen benötigt. Wie dies umgesetzt werden kann zeigt Ryan in seinem dieswöchigen Screencast.

Dowload:
Download (36.4 MB, 15:27)
Alternative download für iPod & Apple TV (21.6 MB, 15:27)


Resourcen:

Quellcode:

[bash]
sudo rake gems:install
[/bash]

[ruby]
# config/environment.rb
config.gem "declarative_authorization", :source => "http://gemcutter.org"

# config/authorization_rules.rb
authorization do
role :admin do
has_permission_on [:articles, :comments], :to => [:index, :show, :new, :create, :edit, :update, :destroy]
end

role :guest do
has_permission_on :articles, :to => [:index, :show]
has_permission_on :comments, :to => [:new, :create]
has_permission_on :comments, :to => [:edit, :update] do
if_attribute :user => is { user }
end
end

role :moderator do
includes :guest
has_permission_on :comments, :to => [:edit, :update]
end

role :author do
includes :guest
has_permission_on :articles, :to => [:new, :create]
has_permission_on :articles, :to => [:edit, :update] do
if_attribute :user => is { user }
end
end
end

# application_controller.rb
before_filter { |c| Authorization.current_user = c.current_user }

protected

def permission_denied
flash[:error] = "Sorry, you are not allowed to access that page."
redirect_to root_url
end

# articles_controller.rb
filter_resource_access
[/ruby]

[html]
<!– articles/show.html.erb –>
<p>
<% if permitted_to? :edit, @article %>
<%= link_to "Edit", edit_article_path(@article) %> |
<% end %>
<% if permitted_to? :destroy, @article %>
<%= link_to "Destroy", @article, :method => :delete, :confirm => "Are you sure?" %> |
<% end %>
<%= link_to "Back to Articles", articles_path %>
</p>

<p>
<% if permitted_to? :edit, comment %>
<%= link_to "Edit", edit_comment_path(comment) %>
<% end %>
<% if permitted_to? :destroy, comment %>
| <%= link_to "Destroy", comment, :method => :delete, :confirm => "Are you sure?" %>
<% end %>
</p>

<!– articles/index.html.erb –>
<% if permitted_to? :create, Article.new %>
<p><%= link_to "New Article", new_article_path %></p>
<% end %>
[/html]