If you have been pulling your dick hair out, like i was, i thought i'd post this here, while i'm still in a rage.
Error
When you are running cucumber features, you get the following shit: cucumber features superclass mismatch for class ModelExampleGroup (TypeError)...
I have no idea what this gem does, And i don't want to know. Hell, I only put it there because of a railscast i think, but this gem is fucking me up, so i commented it out of the environment.rb file and everyone is happy. Good riddance.
FiX?
I just installed remarkable-rails instead. i guess the README in the github repo says how to do it. I've not gotten the error since.
So, here i am trying to expand my closed world beyond Restful_authentication and well, basically any other technoweenie based plugin. I try my hand at Clearance
Clearance is a thoughtbot based gem which authenticates users in your web app. Out of the box, it only allows for email addresses as the login ID, which is fine for my stupid app i'm making.
However, in all my years of scaffolding, i guess i've only really used flash[:notice] for displaying my flash messages on an overall layout level. Clearance has flash[:success] and flash[:error].
The reason i found this out is because my clearance_features weren't working and i was getting 8 failures, due to the fact that I was only displaying flash[:notice]. A win for BDD i tell ya.
Gee mister, how'd you fix it
Like all expert coders, i went to the one place I can trust. Twitter. I sent out a tweet which read: twitter ruby fun: lame, hack or on track? < % = flash[:notice] ||flash[:success] || flash[:failure] % >
Most people hated this code, which is fine, as that is what i wanted to hear. While it will work, if i had a need for two flash messages at once, or if i needed to expand my flash message symbols, i'd be fucked. Enter @heycarsten
This dude turned me onto a pretty sweet gist he wrote up for me. A thousand thank you sir's to him.
Gist... Break it down hammer style.
Well everytime you see me, the hammer is on dootie...ok, hammer time is over.
Create a helper called flash_helper.rb and paste this gist into it.
inside the layouts directory, create a folder called partials and a partial inside of your layouts folder called /layouts/partials/flash_message.html.erb.
In your application_controller.html.erb you'll need to add this code to display your new fancy flash messages: < % = flash_messages % >
In your css file, now you can create styles with the id flash_whatever. I used #flash_error{background-color: red;}, etc...If you are new to css, learn up on it son.
Yeah for heycarsten and many thanks to him for allowing me to post this code.P.s., i apologize for this shitty code formatting. blogspot sucks ass.
Scenario - You need some information off of a website, but there are around 100 rows. This information will also need to populate the name column of your model, so you need to clean up the data. Bascially you need to parse information from a paragraph or one big string. This is where hpricot and some ruby come in handy.
html string parser - Rake task
Here is the code i ended up using, and will try and explain it at the end
namespace :sponsor do desc "seed data" task :seed => :environment do require "open-uri" require "hpricot" doc = open("http://www.nascar.com/guides/sponsors/") { |f| Hpricot(f) } @snag = (doc/"#cnnContentArea").inner_html @snag.gsub(/<.*>/,'').split(/\n\t+/).map {|t| t.strip }.reject {|t|t=='' }.each { |t| Sponsor.create!(:name => t) } end end
Code explained
I needed to pull information from that actual url. All of this information lived within the div tag id cnnContentArea. inside the source of the html page, it was prepopulated with \n\t\t\t and the number of tabs varied.
Using hpricot, we open the url page and save it as an hpricot object (see the doc = line above.
Next, we take the saved page and pull out the content from the cnnContentArea div tag, and we save it as an instance variable @snag.
The last chunk of code, does the following:
The @snag string basically replaces itself without any of the paragraph tags (that what gsub does).
Next, the string is split into mini strings based on a delimiter, and in this case anything that matches \n followed by any number of tabs (\t). I was lucky to be able to do it this way.
We then iterate through each mini string(that's what map is doing) and we then strip all whitespace, before and after the words in the mini string. (see the block t.strip
As an added measure, if the mini array is empty, then it is rejected (courtesy of map)
Lastly, we then take the clean mini string and add it to our model, which in this case is a simple Sponsor model under the name column in the DB.
I have to thank emitilin from IRC as he spruced up my code, but i do take solace in the fact that i knew where to go and now i have an even greater understanding of ruby and rails. I'm sure in the comments you can point me to a method that does this in one fail swoop, but when you are learning ruby, you are a winner when you get to apply the knowledge to a problem you need fixed.
You've picked the perfect model names for you new app, but you are unsure when to use a has many :through association or should it be a has and belongs to many association? Or should it be a simple many to many association. Wait, what about has_one and what the hell is a polymorphic anyways?
Well at this point in the game, you may understand what each of the Model Associations do, when you branch out from the basics, or don't use them for awhile, you end up forgetting when and where to use them.
In an effort to streamline the decision of when to use the various Model Associations available in your rails app, I've come up with a decision tree app called Model Association Decider. This decision tree will help you decide when to use a model association in rails. So use this app to determine which association you should use. Model Association Decider
Well here i am. I've taken a good look at what i need out of my life, computeristically speaking, and have decided that I am going to sink even more effort into learning Ruby on Rails, the one true and just framework, praise be unto him.
Thirty Days of Rails.. WTF?
What I've decided to do is document, a post a day for a month, about what i've learned in my rails journey from today moving forward.
I will focus on rails methodologies, techniques, optimization strategies, and anything else that i end up learning.
What the 30 days of Rails isn't...
This isn't a beginners class. I won't explain myself as indepth as I would, had I started from scratch. I'm starting with a loose grasp of rails knowledge obtained from a year and a half of using the framework. That means, i know how to do shit, but i want to learn MORE so that i can create apps trouble-free from here on out. Since I've made it my journey, I will pass these tidbits on to you, the gentle reader.
So let's see where this thing ends up. I take it that I'll probably end up with a lot of posts regarding TDD/BDD testing as i know sweet fuck all about it,however, I will post what i do find out, hoping it helps someone down the road.
From the easy when you finally know what you are doing, column....
How the hell do you setup a select menu in a feature?
Well, it turns out it's easy. Not obvious to me, as nothing is in this testing BDD world, however. Basically For every select menu you want to use in a feature, you must create a Background scenario to populate that select menu before every feature is ran
Here's the code
Feature: Manage Driver lineup as an admin In order to manage all drivers As an admin I want to be able to create and edit a list of drivers.
Background: Given I create a series "Nascar"
Scenario: Login as admin Given I am logged in as an admin When I go to the admin homepage And I follow "New Driver" When I fill in "Name" with "Darryl Waltrip" And I select "Nascar" from "Series" And I press "Create" Then I should see "New Driver saved succesfully"
Nascar and all other known shit in the code above is copyright of their respective entities. I am just using it as an example, so chilax
Now in the background step you can just create the list of series(as i have it) using factories as so...
Given /^I create a series "([^\"]*)"$/ do |arg1| Factory(:league, :name => arg1) end
Enjoy. yes it was easy, just no one told me how to do it, so i thought i'd see if i could help another soul out.
Ok, this may just be an aha moment for myself, but i always thought that when i looked at ryan's screencasts on Cucumber we're overly weird. That's right, i said it.
Now, why were they weird? Well, he started out his steps by including the following kind of Given statements: Given I have zero articles # or Given I have an article named "Cheese"
Without this step at this time, there is no way to follow a article_path("blah") link at all. In my simple mind's eye, i thought there would be.
Lessoned Learned?
Stop being a moron and setup your Given statement to either start with a record or two for the model you are trying to BDD against, or make sure NONE exist, to start with a clean slate.