How can I test ActionController extensions within Rails 3.2? - ruby-on-rails-3

I wrote an extension for ActionController::Base in Rails 3.2. Now I want to test it. But because the gem I created is no application it has neither specific controllers nor routes, so I cannot use "get" or "post" to call any action.
Is there any module which can create generic controllers on the fly (like acts_as_fu does with ActiveRecord models)? Or is there any testing pattern I can relate to?
I have found this article which explains how to test a before_filter. So far so good although the solutions are to some extend heavy-weight or unclean.
Edit: In particular I want to test an around_filter (if it works and if it redirects correctly) which is created by my extension.
I'm looking forward to your answers.
Thanks and kind regards.

Related

Where is the defination of rails validators?

I am a beginner in rails. As I was working on rails validation, I wanted to know where is the definition of valid? and new_record? and all the validation helpers?
I was trying to find the definition of all these functions but couldn't find any.
Where do all this validations reside? As I know Ruby on Rails is pure object oriented programming language.
Any help will be appreciated. I already went through
http://guides.rubyonrails.org/active_record_validations.html
Thank you.
These are Rails methods:
You can find valid? here and its code on Github.
Likewise with new_record?, you can find a description and its source code here.
Also, here is a link to the Rails repository on Github.
These methods are not defined in the project, they are defined in the rails gem which you have in your Gemfile.

Ruby on Rails - using a Themeforest Admin Theme with my project

I am trying to build a CRM tool for a particular niche. I'm a complete newbie. This will be my first app. My only programming experience is with VB and MS Access, so RoR is presenting quite the learning curve. I have worked through the first version of Michael Hartl's Rails Tutorial. (And actually absorbed about 10% of it)
I bought a Themeforest bootstrap admin theme and have two general questions on it:
1) The theme has some PHP code in it. I'm assuming that I should re-write this code in Ruby, right? The code mostly controls things like file uploading, etc.--things that could be re-written in Ruby by an experienced programmer rather quickly (for me, it will take months :)
2) The theme contains a number of different pages. For example, one page is a dashboard, and another is a calendar. Both of these pages have a lot of duplication between them - all the main control buttons, etc., stay the same from page to page. In the theme, each page is a different HTML file where all of the code is simply duplicated. I'm assuming that I'll want to set up some sort of template system in Rails so that I don't cut-and-paste code between a bunch of HTML pages, right? (If I change a main button, I only want to make that change in one place, rather than in each of the 20 HTML files that came with this theme.)
1)
I'm not absolutely sure if there's no other way, but it's most likely the easiest solution.
If you have to rewrite stuff that's common in web apps, like file uploading, there is usually a gem to help you out, so you don't have to do everything from scratch. I can recommend the paperclip gem, Railscast for file uploading, since we use that in our own project.
Note: The Railscast is out of date, so the installation stuff is no longer accurate. Also, paperclip requires ImageMagick to work.
Railscasts also cover lots of other useful gems. If you need to find something specific, just google it. The github page then usually reveals if a gem is still maintained or if you're better off with something else.
2)
Rails prevents duplicated code with partials. Here's the Railscast (syntax might have changed since 2008). Partials let you place code like headers, or buttons in your case, in a file, which can then be rendered in any of your views.
Unfortunately, I can't link the other stuff like the github page and Rubygems.org because I lack the reputation. I hope this still helps a bit.
Extract the common elements of the theme into your application layout.
Extract the modular sections of the theme into their own controllers and actions. For example, create a calendar controller for the calendar section, the actions that appear in the calendar controller will be the views that support the calendar. You can also use partials (views that start with '_'), without having to create a controller action. But if there is data that needs to be sent to the view, it is better to stick with normal controller actions and views.
As for the php code, get rid of it, move as much logic as possible into your model and controllers, with preference to putting in your models. As hobo suggested, check out gems to replicate the functionality of any complicated php code.

ruby on rails - create a simple html page

I need some assistance in creating a simple html page and locate it in the server.
Since I couldnt find it in the doc I tried to put a index2.html file in the /public library and when I tried to get receive it using
http://127.0.0.1:3000/index2.html
or
http://127.0.0.1:3000/public/index2.html
and receiver
ActionController::RoutingError (No route matches [GET] "/public/index2.html"):
how do I get the html from the server
how di I make it the default.
Thanks.
EDIT : thanks for your answers. I am looking for the simplest way to work. in many tutorials it says you can put it in the public folder and thats it. This will same me useless controllers.
You can do that in several ways : the more obvious would be to have an action in one of your controller (or a new one, whatever) that does nothing (except render the view), and add a route to it. There is other ways to achieve this too, with more adapted tools. I never used any of them, so might want to google it (static page rails), but I know one of them is a gem called High Voltage. Hope this serves you well.
I think you might have mis-understood Rails...
I have never used it but I think I can probably help you with your answer. Rails is the most popular MVC framework for the language Ruby, hence 'Ruby on Rails'. Here is a link to the MVC framework this may help you understand it:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
With the MVC framework you can't 'get' a file directly through the URL. You have to get it through the routing engine. Usually you do this through a Controller which renders a view, typically, from a model.
It's difficult to explain if you don't understand how the MVC framework works but once you have an understanding you should be able to develop web applications very rapidly.
Have a look here for creating your first rails app:
http://guides.rubyonrails.org/getting_started.html

How would I test controllers in Rhomobile in a BDD style?

I am using the Rhomobile framework to develop an application that is supposed to run on different mobile phones. The tutorial mentions how to write tests for the example model: Person. In fact, the framework provides a command-line tool rhogen spec to generate the dummy tests for the models. How would I go about writing tests for the controller class: PersonController? I was thinking of something similar to how one would do it in Rails, but rspec is unknown to me, and the methods post and redirect_to doesn't seem to work.
In BDD when you want to specify the behavior of the application (this is what controllers are responsible for) - you would normally aproach that with a StoryBDD framework - use for example Cucumber . Rspec is SpecBDD and regards a different level of abstraction - it is for specifying how classess communicate with each other and what they do.
You can find many tutorials on Cucumber's github page. This one could be helpful

Rails 3, too many ways to test?

If you were just starting out in rails which path would you encourage new users to go down with regards to Testing.
Anything I read about regarding Rails 3, tells me I should be using Rspec 2. But Rspec comes with a whole whack of other things I need to learn like
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy'
And then one of my friends just told me that I should just stick to the testing framework that comes with rails, and maybe just integrate 'shoulda' into the test process because it has a nice syntax.
When it comes to testing it seems like we have way too many options and too many ways of doing something. Maybe this is a good thing, but I always thought rails was about creating common conventions and avoiding doing the same thing in different ways.
I'm fairly new to rails.
I'm unsure where to start.
I feel overwhelmed.
Is this normal?
Since you're beginning with rails, I'd recommend using something that "just works" to get in the habit of testing your code and understanding how to work with your framework. Don't waste your time comparing solutions, choose one and stick with it for this project.
That being said, I really think it depends on your experience developing software:
if you've never used unit tests for your code, learn Test::Unit and consider adopting TDD. Notice that TDD will need you to dedicate a large amount of time to it at first, but that is paid off when you get used to it.
if you have significant experience with code tests and are just confused on how to start with rails, I recommend following through http://railstutorial.org/book. This is a (free) Ruby on Rails book that uses RSpec, Spork and serves as a great introduction
After developing one project from start to end using the testing platform you choose now, you'll have a much better task of choosing the testing framework you wish to use.
I'd also recommend you learn RSpec well before diving into Cucumber.
Sounds like you have option overload. Which is common to most people in a lot of different situations even outside of programming. The absolute easiest thing for you to do, would be to go with what Rails sets up by default, which is called Test::Unit
Their official guide introduces it here: http://guides.rubyonrails.org/testing.html
Once you are comfortable with testing using plain Test::Unit style then I would suggest you explore some of the addons your friend mentioned such as shoulda (https://github.com/thoughtbot/shoulda), and factory_girl (https://github.com/thoughtbot/factory_girl)
Good luck!
But Rspec comes with a whole whack of
other things I need to learn like ...
Actually, you don't need to learn any of those other gems to get started with RSpec. Just go with gem 'rspec-rails, and run rails generate rspec:install to set up your project.
A good way to learn RSpec is to look at the code generated by rails generate scaffold. This creates spec files for controllers, views and routes that deal with the standard RESTful actions.
While learning RSpec you can add in other gems as you discover the need for them. Autotest is essential IMHO. Webrat adds useful matchers for view specs. Factory_girl adds support for factories.
Spork is great, but not really necessary until you have enough specs that performance is an issue.
I feel overwhelmed. Is this normal?
Less of this, more of this :)