martes, 15 de enero de 2008

Active_poll Plugin

Finally, I am glad to announce that I have released the active_poll plugin, which by the way, is my first Ruby On Rails plugin. This plugin is intended to provide poll functionality to applications that register the votes associated with users or, if not desired, just count the votes for anonymous participants.

Install plugin

To install it, you have to run the following command:

script/plugin install http://active-poll.googlecode.com/svn/trunk/active_poll

This command will download the plugin code and run a script. The next step is run the generator command:
./script/generate active_poll

This command will run a script that will prompt you which is the name of the model you want to associate with a user vote (typically the User model). If you don't want to track which users made the votes for a particular
poll, you just can configure the poll as anonymous, this way, the
plugin just increments a counter for each selected answer.

This will copy the plugin's files and the corresponding migration into your project. As usual, it means that you will need to run:

rake db:migrate

Create poll

At this point, we got our plugin properly installed. Now, in order to start using it, we got to create our first poll, with the following command:

script/runner vendor/plugins/active_poll/create_poll.rb

The script is intended to configure the poll. It will ask you which are the answers, which is the questions displayed, among other configurations (allows multiple selection, who is allowed to vote: registered users, anonymous, etc.) for the poll.

Show poll in views

For us to see the poll up & running in our application there are three things left to do:
  1. Use View helper. Insert the active_poll helper within the views that we want to show the poll, as it is shown below:

  2. #someview.rhtml (where you want to show the poll)..
    < %= active_poll ( poll_name, { :in_place => true, :redirect_to => some_url, :view_dir => view_directory }
    ) %>
    ..
    Where:

    • poll_name. This is the name that identifies the poll to display. This is the only required parameter.

    • :in_place. Enables AJAX functionality to the poll if it is set to true, otherwise the whole page would be reload while submitting a vote.

    • :redirect_to. This is a url to redirect after the vote.

    • :view_dir. If present, it loads the poll pages (show_poll, already_vote, user_not_logged, etc) from the directory set by this parameter. Otherwise, all the active_poll pages would be take from the views directory inside the active_poll plugin's folder.

  3. Insert act_as_vote_handler statement. You have to append act_as_vote_handler statement (highlighted with red in the code below) to the controller that handles the view (where the poll is displayed). Here we can see an example:

    class SomeController
    ...
    acts_as_vote_handler
    ..
    end


  4. Modify routes.rb accordingly. The acts_as_vote_handler hook defines the ap_vote_registered method which effectivity process the votes. For that matter, we have to enable the route for that method on this controller, which would look like this:

    #routes.rb
    map.connect 'somecontroller/ap_vote_registered', :controller => 'some', :action => 'ap_vote_registered'

Views of active_poll

This plugin contains multiple views, the one that would be rendered (using the helper mentioned in the section above) depends of the circumstances. We describe each one of them here:

  • after_vote.rhtml. Shows a message after the user votes.
  • already_voted.rhtml. Shown when the plugin finds out that you already (by means of database for logged users or cookies for anonymous) voted for the current poll.
  • error.rhtml. This view is rendered by means of an internal error (poll delete while someone is voting, for example)
  • max_votes_exceeded.rhtml. When the user chooses more options that is allowed in the poll's configuration it shows this view.
  • no_vote.rhtml. This is rendered when the user press "Vote" but he/she doesn't select any option.
  • poll_not_found.rhtml. Polls are identified by the name, defined with at the time we run the create_poll.rb script. When we use a the active_poll helper with a poll's name that is not defined in the database we show the poll_not_found.rhtml view.
  • poll_outdated.rhtml. This view is rendered after the end_date (date of finalization) of the poll is reached.
  • show_poll.rhtml. Displays the poll itself, with the appropriate form that submits the vote.
  • show_results.rhtml. This view shows the current results of the poll.
  • user_not_logged.rhtml. It is displayed when the poll is configured to logged users only and the user is not logged in.
For aesthetic purposes, you should redefine all the views mentioned above in a new directory (accessed by means of the :view_dir parameter) contained in your application. Also, you should base your customized views in the templates that comes in the views directory, where it is shown how to pass the parameters to the controller, for example.

Check for logged users

This plugin realizes that the current user is logged in by asking the session for the user_id, in this way:

user_model_id = session[:user_model_id]

If this value is set, then it is assumed that the user is logged, otherwise the user is treated as anonymous.

In particular, this methodology integrates transparently with the restful_authentication which uses the same strategy behind the scenes.

Improvment & Comments

If you have any comments, questions, and/or suggestions please don’t hesitate to send me an email here.

24 comentarios:

diegal dijo...

Qué grande Marce!

Esto va a haber que probarlo. Puede ser mejor que Civic Duty que está discontinuado.

frozeek dijo...

Buena homónimo!!

Prometo incluirlo en cuanto esté a punto!

Congrats!!!

David Furber dijo...

It looks like a great plugin, but doesn't seem to work. When installing it on Rails 2.0.2, first when I press "enter" to use the default User model, it uses "" as the model rather than "User" and throws errors. When I typed "User" the script ran. However, the install script stalled on an error saying that ActivePoll is a reserved word in Rails.

mgiorgi dijo...

Sorry for the delay, I didn't notice your comment before..

Today, I fixed the bug that you mentioned (apparently it behaved strange when you press ENTER, hopping that active_pol took 'User' as the default model).

I probe it with Rails 2.0.2 and worked fine with me (using a 'Suscrbier' model). If you got any trouble don't hesitate to send me an e-mail at active.poll@gmail.com.

Hope it helps, thank you for the feedback ;)
Marcelo.

Unknown dijo...

hi, what's the open-source license for this plugin? :) Can you check it into the repository?

mgiorgi dijo...

Thanks for asking, recently I changed the License to 'MIT', due the limitations of the previous one.

Cheers,
Marcelo

Unknown dijo...

if i have a few poll and i want to show my second poll which is named "2" my view always show me my first poll which is named "1"... got any idea why this happen???

Unknown dijo...

about my last post here, i've found the problem. but then i found another problem, after filling apoll, user will redirected to after_vote.html which display the amount of vote and its name. but each time it's redirected to this page, it always show the wrong data. do you mind telling me which part of it that display the data.

mgiorgi dijo...

hi!, i'm glad that you are using the plugin :D

About the problem of the results displayed, I found a bug on my code (that I will submit this evening): that refresh only the first poll that is defined in your view, with the results assosiated with the poll form that made the last submission. This is the problem that you find ? If it is the case, then you can test it tomorrow (after I submit the patch). I will post it here when I finish those changes.

Thank you for trying it out,
Marcelo.

mgiorgi dijo...

The patch has been commited. Let me know, if it helped.

Marcelo.

Unknown dijo...

yes. that's the problem. in fact, a while after i got the problem i tried to search the problem and at last i find it.
i also have made a bit modification (cause in my case i only need 1 active poll and i want to show it not from the view) by adding active field, which is meant to show which one is the active poll. it works, but i still haven't test all of the function in it.

Thank you,


Adi

Unknown dijo...

by the way, suddenly i think that the poll result might be invalid since the active_pole only check registered user whether he has voted or not. but active_poll didn't check for unregistered user whether he has voted or not. i have an idea by checking the voter IP address before he vote. I think it's a great idea, but it confuses me about how can i check the voter IP...
do you have any idea for this...??

cheers


adi

mgiorgi dijo...

Currently, active_poll has been designed to register votes for: registered users, unregistered user or both for a given poll. For the case of unregistered user (or both), the current implementation stores a cookie to check if the user voted or not. The solution that you mentioned (obtain the IP address of the user and stores it for each poll that the user has voted) is a good one, but it would take a while to implement because i am a little busy :P. Hopefully I will include this change in v2.0.

But, It must register the votes from unregistered users, it didn't worked for you? If it doesn't let me know.

Thanks again,
Marcelo

Anónimo dijo...

Re: The name 'ActivePoll' is reserved by Ruby on Rails.

I got this when trying to run the generate script as described in the instructions after installing the plugin. It says to run:

script/generate plugin active_poll

but it should be:

script/generate active_poll

Anónimo dijo...

has anyone had luck with this in rails 2.1?

i'm getting errors with the view_path on the active_poll helper method.

active_poll ( 'the_best', { :in_place => true, :redirect_to => root_path } )

Missing template ../../vendor/plugins/active_poll/views/show_poll.rhtml in view path /Users/derek/Sites/testpoll/app/views

i've even explicitly defined the route like so:
:view_dir => File.join(RAILS_ROOT, 'vendor', 'plugins', 'active_poll', 'views')

mgiorgi dijo...

Hi Derek,

Sorry for the delay, but I didn't have much time lately :P
You're right, there were some issues with Rails 2.1, I've been working on it and now it is fixed. If you want/can try it, please let me know if you find some other problem. I updated the blog with the minimal changes made on the installation process of the plugin.

Thank you very much for comment this bug,
Marcelo

Unknown dijo...

I have problem.. about session..
u use session[:user_id].. but I use session[:user][:id]

can u make it flexible ??

about view_path, I still error.. I use rails 2.2

mgiorgi dijo...

Hey anton!,

Glad to hear that you are using my plugin ;)

I've committed a fix that makes it more flexible about getting user_id. Now if your controller has defined:

#this must be visible from the view
helper_method :current_user_id
def current_user_id
#return user id someway
end

Then active_poll will use that instead of looking at session[:user_id]. So, in your particular case, you should implement it something like:

def current_user_id
session[:user][:id]
end

that simple ;)

Talking about the views, I've just tried that and I see it worked. Remember: views_dir is relative to RAILS_APP/app/views...

Thanks for using this plugin and let me know if you got any other issue,
Marcelo.

Patrick Espake dijo...

Muito complicado e não é muito pratico para o dia a dia.

Se usa-se uma área administrativa seria melhor.

Mesmo assim obrigado.

mgiorgi dijo...

Hey guys,

Just wanted to let you know that I moved the project here: http://github.com/mgiorgi/acts_as_pollable/tree/master

It should be working with Rails 2.x as I'm using it for one of my projects in that platform.

Don't hesitate to ask me any further questions.

Thanks

Divya dijo...

I am getting this error

Missing template /home/divya/Project/prototypes/learning_manager/app/views//show_poll.rhtml in view path app/views

But I ahven't set view_dir option .Still its looking for show_poll template in app/views .Any clues why this happens?

<%= active_poll ("divya", {}) %>
This is how i ahve written on my views.

Unknown dijo...

Hi Divya,

active_poll's helper method is deprecated, I strongly recommend that you should use the latest release available on: git://github.com/mgiorgi/acts_as_pollable.git

Now the current signature would be:

<%= poll ("divya", {}) %> or
<%= poll (:divya, {}) %>

Besides that, everything else should work the same. Even if you don't have custom views the plugin should provide the default ones for you.

Please, let me know if you got any problem with that.

Anónimo dijo...

Great plugin

redirect_to should be redirect.

michaelvk dijo...
Este comentario ha sido eliminado por el autor.