Freitag, 11. Oktober 2013

The end of the summer

The end of the summer is here and RGSoC finished for our project. This was a very intensive experience. I have learned a lot and met very interesting people of the community.
I say thanks to Mathias  because we has helped me from the first moment.
Also to Benedikt  who was always alert for our necessities. To Anika  who offered me the use of the Travis offices because I didn't have a good place to meet my coach. Also thanks to the people of  who has treated me so well.
Also thanks to each of you that has read my blog, encouraged me and gave me their opinions to improve my knowledge.

But this isn't the end. I continue learning Rails and working with it. So I'll continue writing and sharing  in my new blog Adventures in Ruby Land.

See you in Ruby Land!




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