Permalink
Browse files

Fixing Ruby code identation

  • Loading branch information...
1 parent 5149c94 commit 3af90aef015100b59e9484c1e91bf6e3a5a9e264 Mohamed Magdy committed Jul 15, 2014
Showing with 25 additions and 22 deletions.
  1. +25 −22 Ruby/inherit_from_open_struct.rb
@@ -2,10 +2,10 @@
# or when they're not. This class saves the user's preferred location but without persisting it in the database
class NonPersistedPreferredLocation < OpenStruct
include PreferredLocationValidations # Validations
-
+
# Overriding the save method to return true if the object is valide and the "errors" attribute is not set
def save
- self.errors.blank? && self.valid? ? true : false
+ self.errors.blank? && self.valid? ? true : false
end
end
@@ -18,33 +18,36 @@ def create
initialize_preferred_location
respond_to do |format|
- if @preferred_location.save
- format.html { redirect_to @preferred_location, notice: 'Preferred location was successfully created.' }
- format.json { render json: @preferred_location, status: 200 }
- else
- format.html { render action: 'new' }
- format.json { render json: @preferred_location.errors, status: 406 }
- end
+ if @preferred_location.save
+ format.html { redirect_to @preferred_location, notice: 'Preferred location was successfully created.' }
+ format.json { render json: @preferred_location, status: 200 }
+ else
+ format.html { render action: 'new' }
+ format.json { render json: @preferred_location.errors, status: 406 }
+ end
end
end
-
+
# This method saves the preferred location. In case of a logged in user, the preferred location will be saved
# in the database else it will be saved in the browser's cookies
def initialize_preferred_location
begin
- geography = Geography.find(params[:preferred_location][:geography_id]) # Finding the geography before saving it
-
- if user_signed_in? # Registered and logged in user
- @preferred_location = current_user.preferred_locations.new(params[:preferred_location]) # Save in the database
- else # Non logged in user
- geography_ids = preferred_locations_ids
- @preferred_location = NonPersistedPreferredLocation.new(id: geography.id, geography_name: geography.name,
- preferred_locations_count: geography_ids.count)
-
- cookies[:geography_ids] = (geography_ids << geography.id).to_json if @preferred_location.valid?
- end
+ geography = Geography.find(params[:preferred_location][:geography_id]) # Finding the geography before saving it
+
+ if user_signed_in? # Registered and logged in user
+ @preferred_location = current_user.preferred_locations.new(params[:preferred_location]) # Save in the database
+ else # Non logged in user
+ geography_ids = preferred_locations_ids
+ @preferred_location = NonPersistedPreferredLocation.new(
+ id: geography.id,
+ geography_name: geography.name,
+ preferred_locations_count: geography_ids.count
+ )
+
+ cookies[:geography_ids] = (geography_ids << geography.id).to_json if @preferred_location.valid?
+ end
rescue
- @preferred_location = NonPersistedPreferredLocation.new(errors: t('errors.messages.geography_not_found'))
+ @preferred_location = NonPersistedPreferredLocation.new(errors: t('errors.messages.geography_not_found'))
end
end

0 comments on commit 3af90ae

Please sign in to comment.