concept action in category ruby

appears as: ction, ctions, action, n action, Actions, actions, The actions, The action
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

A couple of sample runs demonstrate the new program in action:

Figure 2.2. Argument assignment logic in action

The basic idea of the case/when structure is that you take an object and cascade through a series of tests for a match, taking action based on the test that succeeds. But what does match mean in this context? What does it mean, in our example, to say that answer matches the word yes, or the word no, or neither?

Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

Oops: it’s not here! The code to find the @purchase object is identical to what you saw earlier in the show action: it’s set in before_action, which runs before the show, edit, update, and destroy actions.

For now you’ll test in a very naive way, by copying and pasting all of the existing tests and code you wrote for the show? action, and then you’ll do some refactoring—the code will be nearly identical, and a lot of the permission stuff you’ll do in the future will be very similar as well.

1) TicketPolicy permissions for anonymous users should not permit action
   :show
   Failure/Error: it { should_not permit_action :show }
     TicketPolicy does not forbid nil from performing :show? on
     #<Ticket:0x007fbf69644030>.

2) TicketPolicy permissions for managers of other projects should not
   permit action :show
   Failure/Error: it { should_not permit_action :show }
     TicketPolicy does not forbid test4@example.com (User) from
     performing :show? on #<Ticket:0x007fbf6b5d3a40>.

Pundit is a gem that helps define what permissions users have on resources and actions through what are known as policies. Policies are checked on a resource-by-resource basis. Pundit is maintained by Jonas Nicklas (of Capybara fame) and his coworkers at Elabs.

Rails 3 in Action

This is an excerpt from Manning's book Rails 3 in Action.

If the return value is true, the action responds by redirecting to the new @purchase object using the redirect_to method, which takes either a path or an object that it turns into a path (as seen in this example). The redirect_to method interprets what the @purchase object is and determines that the path required is purchase_path because it’s an object of the Purchase model. This path takes you to the show action for this controller. The :notice option passed to the redirect_to sets up a flash message. A flash message is a message that can be displayed on the next request.

As your application now stands, anybody, whether they’re signed in or not, can create new projects. As you did for the actions in the TicketsController, you must restrict access to the actions in the ProjectsController. The twist here is that you’ll allow only a certain subset of users—users with one particular attribute set in one particular way—to access the actions.

git add .
git commit -m "Restrict access to project
             actions to admins only"
git push

You should also hide the links from the users who are not admins, because it’s useless to show actions to people who can’t perform them.

Before you write this before_filter, you write a controller spec rather than a Cucumber feature to test it. Cucumber features are great for defining a set of actions that a user can perform in your system, but controller specs are much better for quickly testing singular points, such as whether or not a user can go to a specific action in the controller. You used this same reasoning back in chapter 4 to test what happens when a user attempts to go to a project that doesn’t exist.