ActiveScaffold: singular polymorphic association inline actionlink

3 Comments

Quite a complicated subject for a post. 🙂 Hope at least some of you have an idea about the topic of this post.
Activescaffold supports concept of an inline actionlink, which is basically just a list column as a hyperlink. Out of the box it is used for associations to have a one click path to associated records. Unfortunetly, it was nt supported for singular polymorphic associations and in one of my projects I was missing this feature. I had to implement a log feature using a database as store. Each log statement should include a reference to the source. source was a perfect candidate for a polymorphic association.
I just had to extend activescaffold to support singular polymorphic assocation inline actionlinks.

I ve prepared a simple example app including invoice and applog model for those of you who are interesting in trying out.

Setup Example Application

  1. Download: one_step_active_scaffold
  2. Download: polymorphici_blog.rb and rename it to model_setup.rb
  3. Execute following command: ruby one_step_activescaffold.rb polymorphic_app jquery
  4. cd polymorphic_app
  5. rails s
  6. http://localhost:3000/invoices
  7. Create new invoice with a log entry
  8. http://localhost:3000/app_logs

ActiveScaffold: actions_for_association_links supporting list

9 Comments

Many of you know already that you may configure your inline actionlinks for singular associations columns.
Possible action values are :new, :show, :edit.

Let me show you a simple example. You would like to show “new” form in case player has nt got any team assigned and in case a team is assigned you would like to show “show” form

class PlayersController < ApplicationController
  active_scaffold :player do |conf|
    ....
    conf.columns[:team].actions_for_association_links = [:new, :show]
    ....
  end
end

I m using it quite often, however sometimes I was missing the option to show list view. That s why you have the following options these days: :new, :show, :edit and :list.

class PlayersController < ApplicationController
  active_scaffold :player do |conf|
    ....
    conf.columns[:team].actions_for_association_links = [:new, :list]
    ....
  end
end

Try it out. It s a shortcut for the user, all available information and actions for association model is just one click away and it s inline.

ActiveScaffold: Actionlink Parameters

4 Comments

A real quick one for today.

We will take a closer look at which options you have to pass addional url params to action_links.
My example is based upon my post: ActiveScaffold Confirmation of Actionlinks

Static Parameters

A static parameter is a simple key-value pair inside of a hash you may define for your actionlink:

class PlayersController < ApplicationController
  active_scaffold :player do |conf|
    ....
    conf.action_links.member.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false, :ignore_method => :ignore_fire_action?, :parameters => {:static => 'static'}
    ....
  end
end

Reload players controller in your browser and you see the new parameter ‘static’ attached to each fire action_link.

Dynamic Parameters

Static Parameters are nice, but well they are static.
However, it s also possible to do dynamic one s a s well and actually it s quite easy.

class PlayersController < ApplicationController
  active_scaffold :player do |conf|
    ....
    conf.action_links.add 'fire', :confirm => 'are_you_sure', :type => :member, :method => :put, :position => false, :ignore_method => :ignore_fire_action?, :parameters => {:static => 'static'}, :dynamic_parameters => Proc.new {{:dynamic => random_number}}
    ....
  end
end

We ve added the following to our action_link definition: :dynamic_parameters => Proc.new {{:dynamic => random_number}}
A simple Proc Object which is returning a hash including a key ‘dynamic’ with a value… random_number…. ??
Let me explain: our Proc object is called in scope of your helpers, which means you have access to any helper methods.
Therefore we define a helper method ‘random_number’ in our players_helper:

players_helper.rb
module PlayersHelper
  def random_number
    400 + rand(100)
  end
end

Wish you a great weekend.

ActiveScaffold: Confirmation of Actionlinks

Leave a comment

Today I was called by a user stating that something really bad happened. He clicked an actionlink accidently and now data is a mess.
Well after some investigation I ve asked him: Why did you even press ok in confirmation dialog?
The answer was quite a surprise for me cause he told me that there was nt any confirmation dialog.
First thought of any developer, user is lying, but well sadly I have to admit that he was right. He had javascript disabled and I had to accept that apparently activescaffold was performing the action without a confirmation.

I ve fixed that today. If you update your activescaffold to latest version, you are save.
However, application will display missing routing error in these special cases.

Let me show you how you may change your application to work as expected if javascript is disabled.
In my post Actionlink response and ignore I ve described an easy way to respond to actions triggered by an actionlink. I will use it as a base for my example.

First step: Adding a new route

 put :fire, : on => :member
 get :fire, : on => :member 

As an alternative you might also add non resource routes, it s important to add them BEFORE the resource routes.

match 'players/:id/fire' => 'players#fire', :via => [:get, :put] 
resources :player

Second step: Rewrite action method fire in players controller

def fire 
  process_action_link_action do |record| 
    begin 
      record.update_attributes!(:salary => 0) 
      self.successful = true 
      flash[:info] = 'player fired' 
    rescue 
      self.successful = false 
      flash[:error] = 'Internal Error'
    end 
  end 
end 

New Method process_action_link_action is doing all the magic and in addition should further simplify your action management.