Sonntag, 29. September 2013

Cleaning code: Reducing the number of parameters in functions

This week I have continued refactoring the code in the application translate_german_words. After make smaller functions, I was able to see that there were a lot of parameters in the new functions, so I decided to create new classes. I moved some functionality from the principal script translate_words.rb to some new classes:

read_words_from_file.rb
search_words_in_dictionary.rb
write_words_in_file.rb

I learned: 1 class <-> 1 responsability

Also, I created a config.yml file and with the class load_config.rb, load the configuration into the application.

Also, I removed not needed local variables in the methods, made the readme file more friendly and I got smaller methods.

I only can say that the process is so natural. I mean, you start using meaningful names, making smaller functions, creating classes..., step by step.

At the moment that's all!!

Mittwoch, 25. September 2013

Coding with glamour: Ruby style

Wow, what a week!
I assisted the Baruco conference and then the Monitorama Conference. Very interesting conferences!!

What I have done between conferences?
Speaking with  at Baruco about my code, he told me that my code in the application translate_german_words didn't follow the ruby style. Totally true. So, I decided apply Ruby Style Guide to my small application and I liked the result. I replaced some code lines with functions with meaningful names and I was able to remove comments.

After that, I installed the gem RubyCop to check if there was something else that I didn't make. There were some spaces at the end of lines. Also, the lines are very long. I'll check that later.


Freitag, 13. September 2013

Changing dinamically the terminal title in Mac using the rvm gemset name

I usually work with the Terminal on my Mac to run different commands, so I open different tabs, for example, one for voluntary, another for voluntary_classified_advertising and so on. Mathias suggested that it was a good idea to use a meaningful name as title of each terminal tab, so I can have the title "voluntary" if this terminal tab is in the directory "voluntary". To do that, you should open the Shell option in the Menu of Terminal, and select "Edit Title", and then change the Title.

However I thought: How I can make the title dinamically?
Well, when I change to the directory "voluntary", the gemspec is changed also. To do that I moved to my voluntary directory and executed:
rvm --create use --rvmrc ruby-1.9.3-p362@voluntary

So a  .rvmrc file is created, and it changes the gemset for using ruby-1.9.3-p362@voluntary each time you move to "voluntary" directory.

Then, I wrote the script ~/bin/get_gemset.sh:

#!/bin/sh

DEFAULT_GEMSET="/Users/mcberros/.rvm/gems/ruby-1.9.3-p362"
GEMSET=`rvm gemset name`

if [ "$GEMSET" == "$DEFAULT_GEMSET" ]; then
        echo 'default'
else
        echo $GEMSET

fi

You must execute:

chmod u+x get_gemset.sh

Also, I modified my .bash_profile to add the following lines:

export PATH=~/bin:$PATH
PROMPT_COMMAND='echo -ne "\033]0;`get_gemset.sh`"; echo -ne "\007"'

Important: This line has to be the last in your .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Then, reload your .bash_profile with the command:
source .bash_profile

If you leave the voluntary directory and move to other directory without a specific gemset, then your gemset will be "default" and this will be the title of your terminal tab.

By the way, this weekend I am in the Baruco conference (http://www.baruco.org/) in the beautiful city of Barcelona.

Nice weekend!!!




Mittwoch, 11. September 2013

Problems testing with Rspec

Since yesterday I have been working with Rspec reading and trying to follow the examples in the book "Instant RSpec Test-Driven Development How-to". I have some errors in the testing of validations of the Location model and in the create action in the controller. I'll continue learning Rspec to solve these problems.

Sonntag, 8. September 2013

The mistery of nested forms (III)

On last Friday, I read until the chapter "Writing ActiveRecord specifications" of the book "Instant RSpec Test-Driven Development How-to". I like this book because you can have a fast start for Rspec.

In the evening, Mathias solved some doubts that I had in the nested form view:
- The Task model is Mongo. I tried to use the method accepts_nested_attributes_for in this Model but it didn't work without has_many :vacancies. We didn't wanted this association, so I asked Mathias. The solution was that the Product::ClassifiedAdvertising::Task class in voluntary_classified_advertising implements the methods vacancies and vacancies_attributes=, so we don't need the method accepts_nested_attributes in our Task model to achieve the nested form between Task and Vacancy.

- Another doubt that I had is that the path to access the partial template "_vacancy_fields.html.erb" is "workflow/tasks/vacancy_fields". When I tried to access the partial template "_candidature_fields.html.erb" from "_vacancy_fields.html.erb", I had to use the path "products/types/classified_advertising/workflow/tasks/candidature_fields", although both templates are in the same directory. The solution is that we must implement a method "product" in candidature. This method allows to builds the relative path "products/types/classified_advertising/" for the partial template.

After that, I changed the view from a list of resources to a form for a resource, because what the client asked was a form and not a list. So an user can create a new Resource for an existing Vacancy. The problem is, as we saw in our last post, that Candidatures can't receive the attributes from Resources because there isn't a nested form between both Models. So we have to solve this problem the next day.

Freitag, 6. September 2013

The mistery of nested forms (II)

Last night, after my last post, I continued working because I wanted to complete the update controller. So I studied nested forms with polymorphy. I read code from other developers using nested and polymorphy. And I was able to see that I was making a mistake.

If we want nested forms we have to use the following structure:

(Parent object) has many (children objects)*

I thought our relation was:

Tasks > Many vacancies > Many Candidatures > Many Resources

However, studying our schema and models I was able to see that this relation wasn't right. In fact was:

Tasks > Many vacancies > Many Candidatures

Resource > Many Candidatures

So, we can't apply a nested form from Candidature to Resource, because the parent object is resource and not candidature.

After that, I removed the nested form for resource and wrote a modification in the update controller. If we push the 'Accept' button, we change the state of candidature to :accepted. If we push the 'Deny' button, we change the candidature state to :denied

I have now a view that works, although it isn't finished.
    
* It can be also a has-one relation, but in our case is a has-many relation

Donnerstag, 5. September 2013

The mistery of nested forms

Today I feel happy because I have learned a lot and also implemented a very difficult view without errors. Since yesterday I am implementing the nested form for the vacancy resource allocation through a candidatures casting.

First of all, I finally understood the different kinds of association for Active Records. After that, I was able to pass data between vacancies and candidatures.

Also, I was able to understand the nested form implementation and what happens between the involved models.

My next homework: implementation of the form's controller action.

More adventures tomorrow!

Dienstag, 3. September 2013

Smaller functions and edit multiple elements in a table

Yesterday and today I started making smaller functions in the application  translate_german_words. A consequence of using meaningful names is that you can make a function smaller creating new ones. Also is very helpful understand the different layers of abstraction in your application.
I could see that I was using the same parameter (word) in many new functions, so I changed this functions as methods of the class Word.

So at the moment I was able to see as a natural flow (easier to remember)

Meaningful Names > help making smaller functions > help guessing which function is a method of a class

Trying to make a function smaller I got blocked because there is a big "if" (similar to a case statement), so I'll have to think about it.

Also today I have learning how to edit multiple elements in a table. I should apply this new knowledge in the next task of Voluntary.

Sonntag, 1. September 2013

New model Asset

On Friday, Mathias and I created a new model Asset. In the last posts we did a refactoring for using a Resource (polymorphic) instead of User in Candidature and Vancancy models. A resource can be an User or an Asset.

Today I finished the "Meaningful Names" chapter of Clean Code. Then, solved an error in the application  translate_german_words and finished the refactoring applying this chapter. At the moment, I can see three clear consequences of using meaningful names:
- A developer can read the code easier, like an story.
- You can see easier the repeated code.
- I guess, that it will be useful to make smaller functions or methods (next chapter)

My next task in Voluntary will be apply Edit multiple to create a nested resources table for each vacancy.
Also, I'll continue reading Clean Code and refactoring the application translate_german_words.