Ruby On Rails

Love Ruby on Rails? Love teaching? Like blogging? Want to help contribute back to the Rails community? Look no farther friends, you can do so here!  Email me or comment on this post if you’re interested in contributing links, news,...
Love Ruby on Rails? Love teaching? Like blogging? Want to help contribute back to the Rails community? Look no farther friends, you can do so here!  Email me or comment on this post if you’re interested in contributing links, news, and information to the community.
about 1 hour ago
RT @mental_floss: Why Are There Two Pronunciations for 'G'? —
RT @mental_floss: Why Are There Two Pronunciations for 'G'? —
about 4 hours ago
Three Approaches to Remote Pair Programming - A quick rundown on the current state of the art. Intermediate RubyGem Development - Covering dependency management, testing, and releasing your gem. Ruby on Rails 3 with Salesforce: Conclus...
Three Approaches to Remote Pair Programming - A quick rundown on the current state of the art. Intermediate RubyGem Development - Covering dependency management, testing, and releasing your gem. Ruby on Rails 3 with Salesforce: Conclusion - "Don't do it." am I a woman progammer? - "I am a programmer. I am a woman. Those are two independent facts."
about 9 hours ago
Our friends Ed Finkler and Chris Hartjes recently had a chat about testing in PHP.  Read on to get the low down on different testing tools and their relative merits--check it out as Ed and Chris weep for the future, come to some interest...
Our friends Ed Finkler and Chris Hartjes recently had a chat about testing in PHP.  Read on to get the low down on different testing tools and their relative merits--check it out as Ed and Chris weep for the future, come to some interesting conclusions and get their hands dirty so you don't have to. Ed and Chris had a little chat about testing in PHP. Chris: Okay, so today's topic is PHP testing Ed: Word up Chris: Now, Ed, I know that for the most part you are not a big fan of the mainstream PHP testing tools Ed: Yes, that's true Chris: So what is it that you don't like about them Ed: I guess realistically my complaints are aimed at PHPUnit . It's very powerful and very complete from what I can tell, but I think it's difficult to pick up and I think that difficulty makes people less likely to use it. Because it's by far the best known testing tool, I think that tends to limit the use of unit testing, period, in PHP. That's not necessarily PHPUnit's fault per se. I just think it's the situation we're in. I think the documentation, the setup, and just obtaining PHPUnit is a challenge, particularly when compared to unit testing options I've seen in other languages. Python, for example, has a simple but effective unit testing library built into the core. Chris: So, when you say "difficult to pick up", is it because tests look like this? db = $db; } /** * Turns label values like codingStandardsSuck into * CODING_STANDARDS_SUCK */ public function screamingSnakeLabels() { $results = $db->query("SELECT name FROM labels"); $labels = array(); foreach ($results as $result) { $labels[] = $this->_camelToScreamingSnake($result); } return $labels; } /** * Method that takes a camelCase string into SCREAMING_SNAKE_CASE * * @param string $value */ protected function _camelToScreamingSnake($value) { $result = preg_replace_callback( '/[A-Z]/', function ($match) { return "_" . strtolower($match[0]); }, $value ); return strtoupper($result); } } class DevhellTest extends PHPUnit_Framework_TestCase { public function testShowEdHow() { $db = $this->getMockBuilder('Foo') ->disableOriginalConstructor() ->setMethods(array('query')) ->getMock(); $db->expects($this->once()) ->method('query') ->will($this->returnValue(array('devHell', 'camelCase')); $label = new Label($db); $expectedResults = array('DEV_HELL', 'CAMEL_CASE'); $testResults = $label->screamingSnakeLabels(); $this->assertEquals( $expectedResults, $testResults, "Labels were not correctly coverted to screaming snake case" ); } } Chris: Maybe it's because I've worked with it a lot, all I see is some boilerplate and then a few statements that seem pretty intuitive to me. Ed: I think boilerplate is part of the issue. I think that's intimidating. Tools can mitigate that to some extent, but I don't think it eliminates the problem entirely. I just don't think writing a simple test should be anything more than a couple lines of code. Then you can build upon that iteratively as you need. I think that approach of starting simply and building up your set of tests really helps you understand what's going on, and I think it makes testing a lot more accessible to people who haven't done it before. A lot of testing framework docs I see throw a ton of nomenclature out at the reader. I think if you don't already understand that nomenclature, you won't understand what's up. Chris: So when you say 'nomenclature', you're talking about things like what exactly? Assertions and mocks? Ed: Knowing how to mock that stuff up is pretty complex. In my experience the majority of people w
about 23 hours ago
Lately I've been inspired to test drive my development as much as possible. One thing that is absolutely critical for test driven development is fast feedback from your tests. Because of this, I try to remove the "Rails" dependency as of...
Lately I've been inspired to test drive my development as much as possible. One thing that is absolutely critical for test driven development is fast feedback from your tests. Because of this, I try to remove the "Rails" dependency as often as possible from my tests so I don't have to wait for Rails to load for my tests to run. Sure, I could use spork or zeus to pre-load Rails, but I find that those tools don't always reload the files I'm working on. Besides, I believe that much of your application should be plain old ruby objects that are well designed. One of the things I continually bump against with isolated tests is that there are a few things that I always have to do to get my isolated tests to work. Since we are accustomed to requiring spec_helper.rb or test_helper.rb for tests dependent on Rails, I decided to build a helper for when I run isolated tests to just load some niceties that make running them a little easier. So, here's the full code from my isolation helper (this one works with RSpec). # spec/isolation_helper.rb DO_ISOLATION = ! defined?(Rails) def isolate_from_rails(&block) return unless DO_ISOLATION block.call end isolate_from_rails do require 'awesome_print' require 'active_support/all' require 'ostruct' ap "You are running isolated from Rails!" # swallow calls to Rails class ::Rails def self.root; File.expand_path("../", File.dirname(__FILE__)); end def self.method_missing(a,*b); self; end end # Some RSpec config RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.filter_run :focus => true config.run_all_when_everything_filtered = true end end I'm going to walk through this a little as so that you can understand why I'm doing some of these things. First off, I don't want to run isolated if Rails is actually loaded. This ensures that my tests pass when run as a whole and by themselves. So the DO_ISOLATION constant just lets me know whether Rails is loaded or not. The second part of the helper is where I setup a little method called isolate_from_rails. This is just a method that can be used to hide things from Rails during my isolated tests. For example: require 'isolation_helper' isolate_from_rails do class Product; end end describe MyProductValidator do it "ensures that invalid records return false" do Product.stub :find_by_name { OpenStruct.new(:product, :name => "invalid", :valid => false) } pv = MyProductValidator.validate Product.find_by_name("invalid") pv.valid.should be_false end end This is obviously a contrived example. But what I want here is to make sure when I run in isolation that I have the behavior of my models stubbed correctly. So I isolate the model from Rails and stub the behavior I want to test against. This test will pass whether Rails is loaded or not. (I know, this is just testing my stub, but I'm attempting to demonstrate an example, not real running code.) The next thing is I do in the isolation_helper is use the isolate_from_rails method to setup some commonly used things I use in my isolated tests. First are the requires. awesome_print is a handy gem that makes it easy to spit stuff out to SDTOUT in a pretty way. Think pp on steroids. Then I load active_support. This one is optional, but I find that active_support really doesn't take all that long to load and it's worth it to be able to use all the niceties that Rails provides such as blank? and present? methods. The ostruct library is very nice for stubbing or mocking object dependencies using the OpenStruct class. I've already given an example above of how nice it can be for quickly stubbing out a model, but it works great for just about any object. Next I just put a friendly reminder out there using awesome_print that I'm running isolated. One of the biggest annoyances with running isolated tests are logging statements. If I'm testing a class in isolation, I don't really want to see the output of al
about 24 hours ago
UnQLite - Embeddable NoSQL database engine. LicenseFinder - Help your lawyers figure out how your gems are licensed. This stuff is, by the way, an utter mess in the ruby community. "Confident Ruby" now in Beta! - New ebook from Avdi Gr...
UnQLite - Embeddable NoSQL database engine. LicenseFinder - Help your lawyers figure out how your gems are licensed. This stuff is, by the way, an utter mess in the ruby community. "Confident Ruby" now in Beta! - New ebook from Avdi Grimm. ish. - Responsive HTML design tool that throws random viewports at your code so you can see how it copes (or doesn't).
1 day ago
This article is heavily styled and is best viewed at PeepCode! Written by Geoffrey Grosenbach · Design by Paula Lavalle The only thing more difficult than learning something new is tea...
This article is heavily styled and is best viewed at PeepCode! Written by Geoffrey Grosenbach · Design by Paula Lavalle The only thing more difficult than learning something new is teaching it. At PeepCode, we put a lot of time into creating great explanations of difficult concepts. We won’t publish a video until we’re confident that it explains the topic better than anything else out there. And feedback from people who have viewed our videos on Ember.js, Ruby on Rails, JavaScript and other topics show that we’re doing just that. Summary Here’s some of what we’ve learned about explaining technical topics to developers. Teach one thing at a time Tell a story Use appropriate terminology Know your audience The Goal The goal of any tutorial is to get back into the mind of someone who doesn’t understand the topic. If an explanation only makes sense to people who already know what you know, then it’s not a very good explanation. But that insight alone doesn’t tell you how to explain well. Instead, it might help to think about how developers go wrong when trying to explain a concept. They try to teach too many things at once. This overwhelms the student. They are disorganized. Teaching things out of order makes the student do too much work to sort it out in their mind. They use the wrong words. Throwing an unknown term into an explanation can intimidate a student or even cause fear. They don’t know who they are speaking to. I don’t know of any single explanation that works for all people at all levels of learning. When developers are unclear about their audience, they risk delivering ideas that will confuse everyone listening. Here are four solutions to these problems. Teach one thing at a time This is the biggest problem I see, and it’s the first strategy you should use to improve your explanations. Decide what you’re trying to teach, and only talk about that. You’ll have to intentionally block out the 10 other things you want to communicate. If a concept is important enough, dedicate a separate chapter, slide, or day to it. Then focus on the one thing you’re trying to teach right now. This is extremely difficult for developers. We love details. We love to debate some tiny nuance of a program before it even runs. We like discovering edge cases that no one else has thought about. And we’re afraid. We’re afraid that when we conclude a presentation, one person in the audience will be able to say “Hah! You forgot to mention X!” Or “You were wrong on this one point!” To teach well, you need to get over these fears. You need to allow yourself to say something that’s 99% true, even if you know someone will correct you for the 1%. Because mentioning that 1% in your explanation will probably hinder you from sticking to teaching just one thing at a time. To teach well, you need to abandon your insurance policy of saying “We’ll cover this other thing later.” That disclaimer is only there to comfort you, not to help the student. Teach the one thing you’re trying to teach now, then teach one other thing later when you’ve finished teaching the one thing you’re teaching now. In fact, leaving out an important concept or saying something that’s only 99% true can be used to your advantage. It can help you tell a story. Tell a story with problems and solutions Stories are great because they hold our attention. The story could go anywhere, but we also have a hunch about what will happen next. That anticipation keeps our interest: “Will it turn out like I think it will, or will something unexpected happen?” Malcolm Gladwell is a great
2 days ago
When you develop a program in a group of programmers, it is really important to have some standards. Especially helpful are standards of naming things and formatting code. If all team members format the code in the same way and use consi...
When you develop a program in a group of programmers, it is really important to have some standards. Especially helpful are standards of naming things and formatting code. If all team members format the code in the same way and use consistent names, then it is much easier to read the code. This also means that the team works faster. The same rules apply when you develop software in Python. For Python there is a document which describes some of the most desirable style features for Python code Style Guide for Python Code. However there are some problems with that, as even the standard Python library has some libraries which are not consistent. This shouldn’t be an excuse for your team to be inconsistent as well. Ensuring that the code is nice and readable is worth working for a moment on that. In Python there are two tools which I use for writing code in Python – Python style guide checker and Python code static checker. pep8 Program pep8 is a simple tool checking Python code against some of the style conventions in PEP 8 document. Installation You can install it within your virtual environment with simple: pip install pep8 Usage Let’s test the pep8 command on such an ugly Python file named test.py: "this is a very long comment line this is a very long comment line this is a very long comment line" def sth ( a ): return "x"+a def sth1 ( a,b,c): a+b+c The basic usage of the program is: pep8 test.py The above command prints: test.py:1:80: E501 line too long (100 > 79 characters) test.py:2:1: E302 expected 2 blank lines, found 0 test.py:2:11: E201 whitespace after '(' test.py:2:14: E202 whitespace before ')' test.py:2:8: E211 whitespace before '(' test.py:3:16: E225 missing whitespace around operator test.py:4:1: E302 expected 2 blank lines, found 0 test.py:4:11: E201 whitespace after '(' test.py:4:13: E231 missing whitespace after ',' test.py:4:15: E231 missing whitespace after ',' test.py:4:9: E211 whitespace before '(' test.py:5:6: E225 missing whitespace around operator test.py:5:8: E225 missing whitespace around operator test.py:6:1: W391 blank line at end of file Configuration Pep8 is highly configurable. The most important options allow to choose which errors should be ignored. For this there is an argument --ignore. There is also one thing in PEP8 document, which I don’t agree with. This document states that the length of the line shouldn’t be bigger than 80 characters. Usually terminals and editors I use are much wider and having 100 characters doesn’t make your program unreadable. You can set the allowed length of your line with --max-line-length. So if I want to ignore the errors about empty lines at the end of file and set maximum line length to 100, then the whole customized command is: pep8 --ignore=W391 --max-line-length=100 test.py The output is different now: test.py:2:1: E302 expected 2 blank lines, found 0 test.py:2:11: E201 whitespace after '(' test.py:2:14: E202 whitespace before ')' test.py:2:8: E211 whitespace before '(' test.py:3:16: E225 missing whitespace around operator test.py:4:1: E302 expected 2 blank lines, found 0 test.py:4:11: E201 whitespace after '(' test.py:4:13: E231 missing whitespace after ',' test.py:4:15: E231 missing whitespace after ',' test.py:4:9: E211 whitespace before '(' test.py:5:6: E225 missing whitespace around operator test.py:5:8: E225 missing whitespace around operator Config file The same effect can be achieved using a config file. PEP8 searches for this file at the project level, the file must be named .pep8 or setup.cfg. If such a file is not found, then it looks for a file ~/.config/pep8. Only the first file is taken into consideration. After finding a file, it looks for a pep8 section in, if there is no such section, then no custom settings are used. To have the same settings as in the above example, you can create a file .pep8 in the project directory with the following content: [pep8] ignore = W391 max-line-length = 100 T
2 days ago
Searchlight - Searching gem that minimizes magic. Comes with ActiveRecord integration module. Open Source Report Card - Analyze a GitHub user's activity for interesting patterns. Fargo - Online outliner that stores its files in your Dr...
Searchlight - Searching gem that minimizes magic. Comes with ActiveRecord integration module. Open Source Report Card - Analyze a GitHub user's activity for interesting patterns. Fargo - Online outliner that stores its files in your Dropbox.
2 days ago
Easier rules for class structure, ProMotion for RubyMotion, JSON APIs in Rails 4, concurrency with Futuroscope, ActiveRecord help via Searchlight, and internationalization with haml-i18n-extractor. Listen to this episode on Ruby5 This...
Easier rules for class structure, ProMotion for RubyMotion, JSON APIs in Rails 4, concurrency with Futuroscope, ActiveRecord help via Searchlight, and internationalization with haml-i18n-extractor. Listen to this episode on Ruby5 This episode is sponsored by Heroku Build better apps, faster. Spend your time building applications, not managing the servers that run them. Heroku is a Platform as a Service that adds simplicity to deploying, scaling and managing the infrastructure that runs applications. It removes barriers that slow the software development process, freeing you to put 100 percent of your energy into creating quality software. Sandi Metz's Rules for Developers Sandi Metz, author of Practical Object-Oriented Design in Ruby, has put together some simple rules of thumb regarding class and method composition. Caleb Thompson wrote up a blog article about them over at the Thoughtbot blog. ProMotion To make it even easier to build iOS apps in Ruby, Jamon Holmgren created ProMotion, a RubyMotion framework for abstracting the screen and navigation handling in a Ruby-like way. It makes it easy to work with things like UINavigationControllers, UITabBarControllers, and UIViewControllers. JSON APIs with Rails 4 Emil Soman wrote up a blog article on how to write a tested, documented and versioned JSON API Using Rails 4. Futuroscope Josep Jaume Rey of Codegram wrote in to let us know about Futuroscope. It's their implementation of "futures", which let you easily handle processes in the background. You just wrap your long-running code in a block, and retrieve the result when you need it. Searchlight The Searchlight gem, which simplifies complex method chains. It works with ActiveRecord and ActionView out of the box. It also lets you set defaults and offers other shortcuts for interacting with Rails forms. haml-i18n-extractor This week Shai Rosenfeld released the haml-i18n-extractor gem, which is a command line tool that looks for certain strings in your haml templates that are likely to be internationalized. It replaces those strings with calls to the translate ("t") method, then it creates the proper yaml locale files for you.
2 days ago