Coderbyte programming challenges!

“Essential training for working with strings, numbers and arrays.”

Today I was checking my old bookmarks and came across Coderbyte. It is a website full of programming challenges. Many of the challenges can be considered as essential training for working with strings, numbers and arrays. There are easy, medium and hard difficulty challenges. I came across such challenges in the past, when I was coding with C and Javascript. However this time I am going to be using Ruby.

By using Ruby, I found challenges easier than coding in any other language. For example the first challenge named “First Reverse” was really simple with Ruby. This challenge was asking for a code segment that will be taking a string and return it in its reversed order. Well, I immediately though, OK I surely cannot use the “reverse” method, can I? But I thought I will try it as I am using Ruby… why not! Guess what; it worked!

So far I have completed the first four programming challenges (solutions can be found on my github account). Some of them took more time than I expected. However, I was in need to do a lot of digging into the Ruby libraries and also refresh my “coding” logic. I will definitely solve all the easy challenges as are a good practise! In addition, all these challenges will surely come handy with future tasks.

Refactoring my almost identical controllers

“Refactoring should take place only when necessary, to simplify the code.”

On my newly created self-learning project, 9FUN!, I ended up having two almost identical controllers. These controllers were created after using polymorphic association by creating Posts and then TextPosts and PhotoPosts controllers. These two identical controllers can be found on Github Gist.

I thought that refactoring them is essential as 90% of the code is identical. At first I thought using mixins. However this would create complexity that was unnecessary. So I decided to ask people on reddit and #RubyonRails (IRC) of what they would suggest in such case. The helpers, over the IRC channel, suggested that I should keep the two controllers as is, as they were well structured and no complexity was involved. I headed over to reddit and posted the Gist link there for any other suggestions.

On “/r/rails”, the users gave me several solutions on how to refactor the identical controllers. The solution I found more interesting was using inheritance from the “main” PostsController. Some other solutions were to delete the controllers and create the objects I needed using nested forms (in the PostsController) or extract the extra code into a module (or by using concerns).

My conclusion was that, in my case refactoring was not necessary. The controllers were clean and introduced no complexity. Above all, this situation that generated all these discussions made me understand that refactoring should take place only when necessary, in order to simplify the code for future review or for other developers easily reading your code.

Rails 4 Concerns – great for tidying up!

“Making easily readable code is an important aspect of developing. Here are Rails 4 Concerns.”

In the past, I came across two Rails applications which their user models were really huge and complex. I had hard time understanding them as I was at the first stages of my Rails career.

Today, I have been working with the Thoughtbot’s workshop “Intermediate Ruby on Rails”. At the end of the workshop, Matthew Mongeau talks about concerns. I find concerns a very good developing practise for tidying up your models; especially the user model as it is possibly the fattest one. In addition, concerns can also be used for your controllers.

Rails 4 includes concerns by default. If you create a new Rails 4 application you will notice that Rails creates a folder “concerns” under “/app/models/” automatically.

Here is a great article on how to use concerns, found on RichOnRails.

“bugs fixing” day!

“I thought, that’s it! I declare this as “bugs fixing” day!”

Yesterday I was working on the Voting system until late night. This morning, voting was not working properly. I thought of recording it as yet again another bug. However I was getting really frustrated as the bugs started to pile up. I thought, that’s it! I declare this as “bugs fixing” day!

Fair enough, I fixed all known bugs today!

The voting system bug was causing a routing error. The problem was the wrong set http action “POST” I switched it to “GET” and the error was fixed.

The Devise problem was taking place because of a conflict with the “strong_parameters” gem. Luckily for me, the Devise creators documented this under this article. After a lot of searching around I found this article and resolved the problem.

The last problem with Google Authentication was the one giving me an unusual error message to search for. The error message was “no support email”. After a lot of digging around I found out that other users had this problem too. The solution was pretty simple as to just add an email for the application created on Google’s side.

I am now satisfied and ready to move on!

Working with Active Record Reputation System (Voting System in Rails)

“Active Record Reputation System: Easy to use.”

The last few days I have been working with the Active Record Reputation System. This gem is used as a voting system for any kind of records like posts, videos, etc. I have worked with this gem in the past. It is really easy to use and its library has several methods for, among others, sorting, resetting, and updating voting. As an example you can use a method which sums up all the votes a user received or sort posts in descending order according to their sum of votes.

The first time I used Active Record Reputation System, I followed the screencast found on Railscasts. However, this time I went through the gem’s source code and worked it out on my own (as the gem was updated to a new version since the creation of this screencast). In fact, following this screencast will lead to unexpected errors. For example the method used in this screencast reputation_value_for has now changed to reputation_for.

Of course, as Ryan Bates suggests you could do this voting system from scratch. It is indeed a fairly easy feature to implement from scratch. I have already implemented some of the functionality like up/down voting, resetting of an already cast vote and a limitation that a user cannot vote own post. Next tasks will be to style the voting system and make user aware if already voted by turning the grey icon into green if user has up-voted and red if user down-voted.

You can check the voting system on my self-learning project found on Github.

First thing… Twitter Bootstrap

“First thing to put in place should be the Twitter Bootstrap”

As I stated before, I am working on a self-learning project on Rails. I am more into creating functionality rather than front-end design right now. I would love to implement some animation with jQuery and coffeescript later on. However, my main focus right now is to make the website function.

I have been checking the web application for testing out the new features. However, I didn’t like browsing in plain text and no graphics. I hated that no layout was in place for tidying up the new implementations I was adding. I was in need of a basic layout.
I worked with the Twitter Bootstrap (can be foundhere) before and I thought I will give it a shot, setting it up from scratch. When I was placing things on the website I figured out that it took me twenty minutes to set everything up! I was amazed! I was after something simple, but using the bootstrap I had so much more!

Next time I will be working on a project, the first thing to put in place will be the Twitter Bootstrap or any other gem that will provide the front-end side of the website!

You should become a Git expert before using it!

“Become a Git expert and save your day!”

A while ago, I was working on a project in beYou media. Back then, I was a trainee there. I was asked to use Github for uploading my work on a private repo. At first, I found the git tool easy to work with. I read couple of git cheat sheets, watched “Try Git” and “Git Real” on Codeschool and experimented a little bit with the git commands on my own. I was no Git expert. I just knew how to pull, push, commit, make/delete branches, etc.

As time went by, the project was getting bigger and I was working on three different branches at the same time. As a result, lot of challenges came up; especially when I was in need to push a branch in production which was in the middle of other releases of the web application. I confess that I was really confused. The result was a real mess. All these merges that occurred were catastrophic. At the end, I decided to create the new branch from master (HEAD) and apply the changes again one by one.

Well, today I watched “Git Real 2” on Codeschool. I was shocked. This course can really make you an expert in Git! I figured out that, back then, couple of commands would save my day (or week as it took me a whole week to fix the problems)! They could just make everything work fine in five minutes time. These commands could save you enormous effort and time! Oh, and more importantly I would escape the frustration!

I recommend to all Git first timers to firstly master their knowledge of what Git is capable of. Become a Git expert. In that way, when you reach at that point, because eventually you will…, a simple Google search for the relevant command, will save your day! You will need to master it anyway in the future!

Introduced to Polymorphic Association

“Any future changes will be well structured and clean.”

The Ruby on Rails project I am working on will employ a Polymorphic association. I created a Title Posts model and a Photo Posts model which will be under the Posts model. Using a polymorphic association is a great association of what I am trying to do rather than making extra columns into a single model named Posts. My experience so far is that this kind of association gives a much cleaner approach for developing. Also it can be useful for creating unique database columns for each of the new model.

So far I have introduced this association and I am finalizing the feature related to it. Any future implementations or changes on these models will be well structured and clean.

Hello Ruby on Rails World!

In this blog I will be posting about my my journey into Ruby on Rails. I have decided to create a website similar to 9GAG in Ruby on Rails. However I will be making slightly small changes. As an example, the user will be posting images and suggest a funny title. Other users will be posting their funny titles too on the specific image and all together will be voting for the best one. So far I have worked out the authentication with devise. Also used omniauth for signing in with Twitter and Facebook. I also am planning to  use Google Login. After finishing up with authentications I will move on to implement polymorphism for the posts (TextPost for titles and PhotPosts for images). You can find the project at https://github.com/stefioan/imageT