Monday, 8 May 2017

RSpec before hooks

I assume that everybody is familiar with the famous ruby testing framework rspec.

Let's say that we have this spec structure:

require 'rails_helper'

describe 'search_index' do
  context 'entity A' do
    let(!setup_a){ create :a}
...
    before do
      SearchIndex.delete! rescue nil
      SearchIndex.create
      SearchIndex.import
    end
...
  end
...
  context 'entity B' do
    let(!setup_b){ create :b}
...
    before do
      SearchIndex.delete! rescue nil
      SearchIndex.create
      SearchIndex.import
    end
...
  end
end

As we see the before blocks are the same. Some might be tempted to move to a common place.
Let's do this:

require 'rails_helper'

describe 'search_index' do
  before do
    SearchIndex.delete! rescue nil
    SearchIndex.create
    SearchIndex.import
  end

  context 'entity A' do
...
...
  end
...
  context 'entity B' do
...
...
  end
end

At this moment we experience a surprise the the tests are not passing...
If we take a closer look we can easily identify the spot.
The import in the before blocks was run before the setup_b and setup_a was called. So there was nothing to import therefore our expectations run on an empty index.

In this particular case it was much more beneficial to leave the common code where it is.
But wait... Aren't we dealing with a ruby code? Yes of corse. This means that the content of the before can be extracted into a function then called before any context.


require 'rails_helper'

def reinitialise_the_index
  SearchIndex.delete! rescue nil
  SearchIndex.create
  SearchIndex.import
end

describe 'search_index' do
  context 'entity A' do
    let(!setup_a){ create :a}
...
    before do
      reinitialise_the_index
    end
...
  end
...
  context 'entity B' do
    let(!setup_b){ create :b}
...
    before do
      reinitialise_the_index
    end
...
  end
end

Much simpler isn't it?
Happy coding :)


Friday, 5 June 2015

Available at Toptal

Hi,

I would like to announce that I joined total.

I am available for hire at:
http://www.toptal.com/ruby-on-rails#efficient-professional-web-development


Wednesday, 6 May 2015

The benefit of the squeel gem over standard active record

The benefit of the squeel gem over the standard active record

Let's try this query:
Field.where(id: 10).unscope(where: :id)
It results in the below SQL:
"SELECT \"fields\".* FROM \"fields\"

Let try to unscope something a bit more difficult:
Field.where('id > 10').unscope(where: :id)
In this case the unscoping didn't work as we intended:
SELECT \"fields\".* FROM \"fields\" WHERE (id > 10)


Let's try now by using squeel gem:
Field.where{id > 10}.unscope(where: :id).to_sql
Now the unstopping works just as we intended:
"SELECT \"fields\".* FROM \"fields\""

Enjoy :)

Friday, 16 January 2015

Testing the ActiveRecord models using Rspec

Usually the skeleton of my models spec looks like this:

require 'spec_helper'
                        
describe Car do
                        
  context 'class hierarchy' do
    #Here comes the class hierarchy specification
  end
                        
  context 'fields' do
  end
                        
  context 'assotiations' do
    #Here comes the enumeration of associations
  end
                        
  context 'validations' do
    #Here comes the validation of models
  end
                        
  context 'callbacks' do
    #Specs for callbacks
  end
                        
  context 'methods' do
  end
end

Testing the class hierarchy:
Since the model could include modules which affect the functionality I consider it necessary to assert on them.

describe Car do
  context 'class hierarchy' do
    specify {expect(subject.class).to be < ActiveRecord::Base}
    specify{expect(subject).to be_kind_of(Elasticsearch::Model)}
    specify{expect(subject).to be_kind_of(Elasticsearch::Model::Callbacks)}
  end
end

Testing the fields:
I often encountered errors when the model was expected to have a field and that field was missing. So I always assert on the used fields:

describe Car do
  context 'fields' do
    specify {expect(subject).to respond_to(:name)}
    specify {expect(subject).to respond_to(:filter)}
  end
end

Testing the associations:
I test the presence of the correct associations using the 'shoulda-matchers' gem.

describe Car do
  context 'assotiations' do
    specify { expect(subject).to belong_to(:user) }
    specify { expect(subject).to belong_to(:manufacturer) }
  end
end

Testing the validations:
I usually test the validations of the fields using the 'shoulda-matchers' gem.

describe Car do
  context 'validations' do
    specify { expect(subject).to validate_uniqueness_of(:filter).scoped_to(:manufacturer_id)}
  end
end

Testing the callbacks:
My callback test usually are like this:

describe Car do
  context 'validations' do
    context 'before_destroy' do
      specify 'call destroy like callback'do
        expect(equipment).to receive(:destroy_likes)
        equipment.destroy
      end
    end
  end
end

Testing the methods:
Usually I test the methods by calling it the asserting that all the necessary changes are made. I do this for all the execution paths.


Thursday, 15 January 2015

Api Development in Ruby On Rails

Recently I wrote a series of blog posts about the best practices of API development in RoR and how to develop API in general.

Let me summarize those posts:


Happy API development :)

API development in Rails error path


In my series of of API development I haven't covered the error handling.

Let me share my experience with you about what I have learned about proper error handling.
I call error path the case when the user can't achieve what he wants.

This case can happen by the following reasons:

  • The user calls an invalid url
  • The user addresses a missing resource
  • The user misses a mandatory parameter
  • The uses passes a wrong parameter
  • Some other internal error occurs
  • ...

Since thousands of calls can be made daily or hourly against the API there is no way to stop the server and debug it. So all the information related to the erroneous call must be recorded. Basically all the information to reproduce the error must be recorded. I call this ApiCallAudit.

Such an ApiCallAudit must contain:

  • All the incoming parameters
  • The type of call (GET, POST, DELETE, ...)
  • cookies
  • backtrace
  • created_at
I added some additional fields to it for filtering purposes:

  • level. It serves to quickly determine the possible source of errors. It could be parameter_error, unexpected_error, parameter_missing_error, entity_missing_error
  • status. It is a default error message.
  • code. An error code which uniquely identifies the error.
In the beginning the error code was missing from my design and the mobile clients were using the default error message. This has some disadvantages:
  • The mobile UI is usually developed in a different codebase. And the server side error message modification is not possible. Specially if the same chunk of server side code is serving many applications.
  • -The language used on the mobile UI can vary. For example the UX developer can decide to use:
    1. "You haven't provided the group" - First person complaining style.
    2. "The group is missing" - Passive objective style
    3. "Please provide a group" - Proactive gentle style
    4. "Select a group!" - Imperative style
    5. ...
In order to formulate the sentences which reflects the application mood the mobile developer needs to interpret the returned error based on its error_code and formulate its own corresponding message.

Happy coding :)



API Development Restfull vs Facebook style


In the last months I developed mainly API's using grape gem and their related gems.

In this blog post I will express my opinion how to organise the API which manages the entities.

Generally speaking I like the REST concept and I organize my API to conform that way. For cases when there are no association among the entities this philosophy is good enough and clear.

However I like the API to reflect the associations when we are dealing with has_many or has_and_belongs_to_many associations. Just like in case of  Facebook Graph API. In these cases I find that style more intuitive and I am following that style whenever I deal with associations.

Examples:

Lets suppose that cars and manufacturers can be reviewed.

class Car < ActiveRecord::Base
  has_many :reviews, as: :reviewable

class Manufacturer < ActiveRecord::Base
  has_many :reviews, as: :reviewable

class Review < ActiveRecord::Base
  belongs_to :user
  belongs_to :reviewable, polymorphic: true

In this case the review API would be:
  • creation of reviews:
    POST /api/cars/{id}/reviews
    POST /api/manufacturers/{id}/reviews
  • retrieval of reviews:
    GET /api/cars/{id}/reviews
    GET /api/manufacturers/{id}/reviews
  • deletion of reviews:
    DELETE /api/reviews/{id}


In the classic restful style the review creation would be:

    params do
      requires :comment, type: String, desc: "The comment"
      requires :reviewable_id, type: Integer, desc: "The id of a reviewable entity"
      requires :reviewable_type, type: String, desc: "The type of a reviewable entity"
    end
    POST /api/reviews

Both solutions have pros and cons. The restful style is more DRY. The Facebook Graph Api style relieves more information about the associations ergo it is more intuitive and easier to use.

Happy coding :)



Friday, 2 January 2015

Specing the API


When you cover your API with specs the first rule is to cover everything. 
Since the API can be called by third parties you need to be sure what is happening in every case. You must be able to reproduce every scenario any time and you must have the same results. :) Does it seems scientific? Well... Indeed it is.

Let me show you some examples:
-You might need to check that only the calls with developer key can access the API. In the opposite case the response is 401, it is still a JSON and maybe an audit log is created.
-You might need to check that the user is authenticated. If not then the response is 401, it is still a JSON and again an audit log is created.
-If some of the parameters are missing or wrong then the response is 400, it is still a JSON and an audit log is created by logging all the incoming parameters.
-If everything is alright then the response is 200/201, the response is a JSON and the entities are massaged as needed by that specific case.

You also need to keep your specs DRY
This will reduce maintenance effort of the test code and it will keep your specs more readable and you will have easy time to add new features and you will feel more happy and in control.

As you can see the above steps like checking the response code, checking if the response format is JSON, checking that the audit log is created are repetitive tasks and can be DRY-ed with rspecs shared examples. The only specific thing which changes from one api endpoint to another is "the entities are massaged as needed by that specific case".

RSpec.shared_examples "returning 401" do
  specify "returns 401" do
    api_call params, developer_header
    expect(response.status).to eq(401)
  end
end
RSpec.shared_examples "being JSON" do
  specify 'returns JSON' do
    api_call params, developer_header
    expect { JSON.parse(response.body) }.not_to raise_error
  end
end
...

Then in your specs you can use these shared examples in particular cases:

context '/API/learn_by_playing/' do
  def api_call *params
    get "/api/learn_by_playing", *params
  end
  let(:api_key) { create :apikey }
  let(:developer_header) { {'Authorization' => api_key.token} }
  context 'GET' do
    let(:required_params) do
      {
        :first_name => 'Botond',
        :last_name => "Orban"
      }
    end
    let(:params) { required_params }

    it_behaves_like 'restricted for developers'

    context 'wrong parameters' do
      required_params.keys.each do |s|
        context 'when the #{s} parameter is blank' do
          let(:params) { required_params.merge({s => ''}) }
          it_behaves_like 'returning 400'
          it_behaves_like 'being JSON'
          it_behaves_like 'creating an audit log'
        end
        context 'when the #{s} parameter is missing' do
          let(:params) { required_params.except(s) }
          it_behaves_like 'returning 400'
          it_behaves_like 'being JSON'
          it_behaves_like 'creating an audit log'
        end
      end
    end
    context 'valid params' do
      specify '...whatever you need to really assert for in this particular case...' do
        api_call params, developer_header
        expect ...
      end
    end
  end
end

As you can see from the above example using Rspec.shared examples a lot of repetitive lines from your Rspec can be thrown out.

On one of my projects I managed to reduce the specs size by 20% and raise the visibility a lot :) I can't measure and therefore I can't express in numbers what "a lot" means but everybody was satisfied  and pleased with the result.

Professional DRY hacking ;)

Monday, 29 December 2014

Use logger.debug rather than puts!

It is often tempting to use puts in the ruby code to debug the informations.

I like logging instead because:
-allows the filtering capability
-allows formatting abilities
-with a little tweak it takes the same amount of effort than puts
-it is an already invented and well tested system

It is much better in short. So whenever is a logger system available I use that.

logger.debug 'doing this and that'

It seem to me too much to type at first, therefore I created a sublime logger snippet which accelerated typing. I created another one for those cases where I need to implicitly reference the Rails.logger.

;)

Sunday, 28 December 2014

API Development in Ruby On Rails, Nested Entities

There are times when there is one-to-many association amongst the entities. And the mobile developer needs to show all of them in one screen. In these cases it is very easy to fell into the bad habit to execute n+1 queries from mobile side toward the server or from the server to the sql-server.

Here are the entities:

class DetailedGameEntity < Grape::Entity
  ...
  expose :reviews, using: ReviewEntity do |game, options|
    game.reviews
  end
end

class ReviewPresenter < Grape::Entity
  expose :user_id, as: :owner_id
  expose :comment
  expose :rating

  expose :image_urls do |review, options|
    review.images.map{|image| image.image}
  end
end

They are designed to return everything the mobile screen needs. The games contains their reviews. And the reviews contain their images. So in one query the whole mobile screen can be populated.

Let's see how a programmer in a hurry develops all this API:

games = Game.actual.limit(500)
present games, with: DetailedGameEntity

By developing the API this way the call will result in these queries:
...
  Review Load (0.5ms)  SELECT "reviews".* FROM "reviews"  WHERE "reviews"."reviewable_id" = $1 AND "reviews"."reviewable_type" = $2  [["reviewable_id", 2359], ["reviewable_type", "Game"]]
  Review Load (0.4ms)  SELECT "reviews".* FROM "reviews"  WHERE "reviews"."reviewable_id" = $1 AND "reviews"."reviewable_type" = $2  [["reviewable_id", 2358], ["reviewable_type", "Game"]]
...

As you can see for each returned game game another select is executed to fetch the belonging reviews. The same is done to fetch the belonging user and to fetch the images belonging to the review. This is a very ineffective way because as the review count grows more and more queries need to be executed. This problem is called n+1 query problem.

I do this in my code in these cases:

games = Game.actual.limit(500)
games = games.includes(reviews: [:images, :user] )
present games, with: DetailedGameEntity

The above code will do this sql query:

 Review Load (1.0ms)  SELECT "reviews".* FROM "reviews"  WHERE "reviews"."reviewable_type" = 'Schedule' AND "reviews"."reviewable_id" IN (2195, 2198, 2197, 9567, 9572, 9573, 9574, 9575, 9576, 9571, 9570, 9569, 9568, 2196, 2204, 2210, 2211, ...

So, the underlying Rails code will execute only one query per entity.

It is nice, isn't it?

Please use it ;)

Saturday, 27 December 2014

API Development in Ruby On Rails, Entities

In my last blog post I presented in general the features I like the most about the grape gem. Now I will present the grape entities which are related to the presentation of the returned data. I like these entities a lot because they are an OO way to present data and they help a lot to keep my presentation layer DRY.

The last statement of every grape call is returned to the caller as JSON.
Despite this simple efficiency I like to have more control over the returned data and I use the grape entities to format the returned data wherever I can. I can also rspec them and I am doing it extensively because I am a Test Driven Guy ;)

Let me present some examples of entities their usage and how do I spec them:

Here is the spec for the entity:

describe GameEntity do
  describe 'fields' do
    subject(:subject) { GameEntity }
    it { is_expected.to represent(:id) }
    it { is_expected.to represent(:user_id).as(:owner_id) }
...


Here is the entity itself:

class GameEntity < Grape::Entity
  expose :id
  expose :user_id, as: :owner_id
...

The part of the grape API:

  namespace :games do
    desc "Retrieve all the games"
    params do
      optional :include_reviews, type: Boolean, default: true, desc: 'Accepts: true/false.'
    end
    get do
      games = Game.actual.limit(500)
      present games, with: GameEntity

This was simple so far. But I can use a more sophisticated GameEntity in case I want to present the included reviews. In all other cases I will use my old simple GameEntity to present the Game.

Let's see that case too:

I modify the grape API to this:

    ...
    if params[:include_reviews]
      games = games.includes(:reviews)
      present games, with: DetailedGameEntity
    else
      present games, with: GameEntity
    end

I also spec the new entity:

describe DetailedGameEntity do
  describe 'special fields' do
      subject(:detailed_game) { DetailedGameEntity(game).as_json }
      specify { expect(detailed_game[:reviews]).to be_an(Array) }
  end

Of course the above was a very simple example.

In real world scenarios we can find ourselves that we are reusing the entities like this:

class DetailedGameEntity < Grape::Entity
  expose :game, using: GameEntity do |game, options|
    game
  end

  expose :venue, using: VenueEntity do |game, options|
    game.the_venue
  end

  expose :visitor_team, using: TeamEntity do |game, options|
    game.visitor_team
  end

  expose :host_team, using: TeamEntity do |game, options|
    game.host_team
  end
end

As you can see we are keeping our entity codebase DRY by calling the TeamEntity twice in the DetailedGameEntity. This is a typical use of delegation of the Entities.

Let me show a more complex example of our entities:

The usage of the entities:

  desc "Retrieve all the reviews belonging to a team"
    get do
      ...
      present reviews, :with => ReviewPresenter, user: current_user
    end

The entity itself:
  class ReviewPresenter < Grape::Entity
    expose :current_user_likes do |review, options|
      review.likes.pluck(:user_id).include? options[:user].id if options[:user].present?
    end
  end

And finally the spec for the entity:
describe ReviewPresenter do
  describe 'special fields' do
    context 'without passing a user' do
      subject(:presented_review) { ReviewPresenter.new(image.review).as_json }
      specify { expect(presented_review[:current_user_likes]).to be_nil }
    end

    subject(:presented_review) { ReviewPresenter.new(image.review, user: current_user).as_json }
    ...
      context 'user is the current_user' do
        specify { expect(presented_review[:current_user_likes]).to eq(true) }
      end
    end
    ...
  end
end

What I was doing here is to populate the field 'current_user_likes'
 to true or false depending if the current_user likes or not the presented review.

As you can see the presentation layer of any grape API could became more DRY and versatile by using the grape entities.

Enjoy using them ;)

Friday, 26 December 2014

API Development in Ruby On Rails

I met a technology and a set of gems which were designed to develop any API in Ruby On Rails about half years ago.

The set of gems were:


At first glance it seemed that it adds complexity to the Rails application compared to standard Rails JSON returns. After I played a bit with it I suddenly realized that its API formulating DSL is much more superior. So I started to like it :)

Let me list the elements of DSL I like most:

  • ability to mount API endpoints. This can be very handy if I want to unmount certain unused parts of the API.

      mount Caesars::Ping
      mount Caesars::PingProtected

  • ability to describe the API endpoint. This description will appear in the Swagger documentation. It is a help for the API user.
  • ability to specify the parameters (type, mandatory or required).
      params do
        optional :user_ids, desc: "JSON array of the user ids to be deleted."
        requires :group_id, type: Integer, desc: "The id of the group"
      end

By specifying the type our parameters will be converted automagically to the specified type. It is easier this way for us developers to write our core logic.
  • a complex DSL to organize the API hierarchy and use route params. I consider this the most powerful feature. I can easily organize the API to look more like Facebook graph API or Philips Hue API than standard Rails restfull API.
      namespace :teams do
        route_param :id do
          desc 'Retrieve all games belonging to teams'
          get 'scores' do
            #DO logic here
          end
        end
      end

The grape features are many more including versioning. Take a look for a complete description at grape site ;)

Thursday, 25 December 2014

Hi,

I have news for you. My daughter had been born in august :) With Her time passed very fast.
I even missed the new macbook release date which was in July. But I don't feel disappointed because according to the rumors the newer MacBook will have a much better CPU. So I will wait for the next generation of MacBook pro :)

Here is a picture about us:


Monday, 1 April 2013

Editing the code with the speed of thought

Hi

I changed my code editor from Textmate2 to Sublime. A friend of mine told me about it and I was so curious that I needed to try it. I started to love it right in the first moment mainly because of the existence of the command palette and its modular feature approach and the multiple editing feature.

The command palette was for me like some sort of help combined with a quicksearch-autosuggest menu. When I thought to something I would do I typed in and if I had luck then the feature was already there. If not then I needed to search for a suitable package which did the desired functionality.
I saw a huge potential in this approach because I always wanted to edit my code like the code editor would be a extension of my body. In other words I wanted to edit code with the speed of my thought.
By default the desired functions were not there. I needed to search for some packages and even edit those packages to achieve my desires. I wanted to toggle between quote types 'a', "a". I wanted to convert a string to a symbol and vice versa "a", :a. I wanted to toggle the do end blocks with the braces blocks. I wanted to convert amongst sneak_case, pascalCase, dash-case, dot.case, slash/separated

So by using it continouosly for more than half year I installed these packages:

          For converting each other from any of them:
    • "sneak_case"
    • "pascalCase"
    • "dash-case"
    • "dot.case"
    • "slash/separated"
  • Clipboard History - for using multiple history feature
  • CoffeeScript - syntax for coffee script
  • Cucumber -  syntax for cucumber
  • Eco - syntax for eco template
  • ERB Insert And Toggle Command
          For toggling amongs these
    • <%= %> 
    • <%-  -%>
    • <%  %>
    • <%=  -%>
    • <%#  %>
  • Gist - I like to store my snippets on gist and access them from sublime
  • Missing Palette Commands
  • Plain Tasks - For storing my tasks in plain text format but nicely formated, like the below list
    • ✔ task 1 @done (13-04-01 21:28)
    •  ☐ implement sorting
  • RSpec - syntax for my specs. I am a TDD advocate. It saves me a lot of time.
  • Ruby Block Converter - for toggling amongst {} block and do end blocks.
  • SFTP - for editing remote files.
  • Toggle Symbol To String for toggling between "symbol"and :symbol
  • Toggle Quotes for toggling between 'string' and "string"
  • Advanced New File for creating deeple nested directories when I place a file into them.
  • Nettuts + Fetch for fetching the latest jquery.js or jquery-ui.js or backbone.js libraries.

Often the commands were missing from some of these packages. So when I searched for 'toggle block' then no result was shown however I wanted to convert a block from "{}" style to "do end" style. This was a bottleneck in my speedy coding... In these cases I needed to open the package add the command to the Default.sublime-commands file and then make a pull request. This way if I eventually forgot the key combination of a feature I could search for it in a command palette.

May I ask any developer who makes a sublime package to add this file to the package and enlist the commands there.
There is the multiple editing feature I like the most... :)
And it is a very fast editor.
I also like the snippet feature of it.

Overall all I can say s that it is a very handy code editor and it worth the money for buying it and spending time to learn it ;)

Saturday, 16 June 2012

Leting your vital javascript functions to do their job when the content changes

Hi Fellow Programmers

In this post I will show you one way to let your pages work as intended even if they are manipulated dynamically.

The problem: When the content of the page changes dynamically, those items are not listened by those javascript functions which were initialized at the time when the page loaded.

At the very first steps every programmer reinitializes only those handlers which are needed by the dynamically reloaded partial. This has some disadvantages. One of them is that if the initialization changes then it is very painful to track back the partials where the initialization need to be changed. Plus, the whole thing need a babysitting process...

I don't want that...

What I want is a structured way, in which is agile and robust enough to handle these issues... :)

Let me show you by code:
window.problemOrNot = { 
  vital_functions: [
    ->
      $(".cancel").click ->
        $('.cancelable_remove').remove()
        $('.cancelable_show').show()

  ]
  run_vital_functions: ->
    $.each problemOrNot.vital_functions, ->
      @()
}

jQuery ->
  problemOrNot.run_vital_functions()
And then in your partial, which obviously modifies your dom:
$('.comments').append("
  • <%=j render :partial => 'form' %>
  • "); ... ... ... $('#comment_operations').hide(); problemOrNot.run_vital_functions();
    That's it... The first code was in cofee script, and the partial was a rails .js.erb partial. :) Enjoy

    Thursday, 7 June 2012

    Full Text Search Feature Of Postgresql

    Hi,

    I was using mysql for my development for a long-long time. But I decided to try postgresql as I watched Ryan Bates screencast which showed the fullt text search capabilities of postgresql.

    I was motivated to try postgresql since heroku added a new addon which enables any application to use the lastest(9.1.13) postgresql. And as a bonus the capacity of that DB is unlimited... However I can hardly believe that it will remain free forever...

    So...
    I created a new DB on my heroku account for my application and I followed the migration manual and voila. It took me about no more than 15 minutes to transition to a new DB.

    It was a nice experience. :)

    Tuesday, 5 June 2012

    Navigating from controllers to .haml type views in Textmate

    Hi, I made a fix which enables your Textmate to navigate from your controller to your haml type views, using the ruby on rails bundle. Enjoy

    Monday, 4 June 2012

    Advanced git features

    Hi everybody,

    I am using git as source control for a long time ago, and I am happy with what it offers. I presume that the basic git commands are clear for everyone. So in this post I will write about some more advanced git commands which aren't used so frequently but they are invaluable when they are needed.


    -Nice Git log which contains a lot of usefull info:
    git log --graph --all --decorate=full --abbrev-commit --pretty=short
    
    -Delete the branch 'staging' on remote 'staging':
    git push staging :staging
    
    -push the branch "newfeature" to the remote "origin":
    git push origin newfeature
    
    -create a remote branch:
    git push  :refs/heads/new_feature_name
    
    -list remote branches:
    git branch -r
    
    -diff between two branches:
    git diff <branchone>..<another branch>
    
    -create a branch wich tracks a remote branch:
    git checkout --track -b <new_feature_name> <origin>/<new_feature_name>
    
    -track a remote branch:
    git branch --set-upstream <local_branch> <remote>/<remote_branch>
    
    -push to a remote:
    git push <remote> <local branch name>:<remote branch to push into>
    
    -configuring to push automatically to a certain remote branch, without specifying the local branch and the remote branch:
    git config remote.<remoteName>.push <localBranchName>:<remoteBranchName>
    
    

    Enjoy and easy your life :)

    Wednesday, 23 May 2012

    Installing Ruby On Rails in windows machines


    I dedicate this post to my friends who can't afford to buy a mac or not experienced enough to work on Linux machines

    In my virtual box I tried to reproduce the installation of Ruby on Rails on Windows XP.
    Because the framework evolved from my last windows installation I realized that some things have become simpler to install and additional things needed to be installed.

    Here are the steps which are needed to get a ready to enjoy Ruby On Rails environment on Windows machines:

    Download ruby:
    http://rubyinstaller.org/
    Install it. (The default instalation directory will be C:\Ruby193)

    Download devkit for ruby form this location:
    https://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe
    Install it to c:/Webkit (important that the directory can't contain spaces)
    Put C:\Ruby193\bin and c:\Devkit\bin in your path. (My computer -> properties -> 'environmental variables')

    Run these commands from a newly opened command prompt:
    cd c:\Devkit
    ruby dk.rb init
    ruby dk.rb review
    ruby dk.rb install

    Now you can install rails by doing "gem install rails". (If you previously tried to install anything without having the devkit installed then please empty your ruby gems directories. Usually they are here: C:\Ruby193\lib\ruby\gems\1.9.1)

    gem update --system
    gem install rubygems-update

    rails new hello_world

    Thursday, 6 October 2011

    Normalize you DB


    Hi everyone,

    In the last couple of days I had the opportunity to normalize a DB. :)

    Let me show you a screenshot about a single table:

    As you see a lot of data is repeated across the rows. Therefore, I had some bad thoughts about those who let the DB become like this in the beginning. I didn't wanted to do it. As the moments were passing away I let the challenge to excite me. At the end I did it.

    Before doing anything the total number of rows in the db was above 100000 and the size of the table was 10,5 MBytes.
    My first step was to extract the name of the components into another table and reference them.
    This way the size of the table decreased to 8.5 MBytes. And the new table size was 1.5 Mbyte. I earned 0.5 Mbyte, not too much, but the database was much more cleaner.
    My second step was to transform the check/change data represented by a string into their numerical representations, this way enabling the database to be internationalized. By doing this the table size decrease to 6.2 MBytes. That means 72% of its original size and the data can be internationalized and we have all the benefits of a normalized DB schema (that means no update problems and a joy to work with).
    I also measured the speed of joins operations. Because of the normalization the same data retrieval was increased by 3 times. I put an index onto foreign keys. And the speed was just like before. :)