Posts

Showing posts from 2015

Integrating google maps in rails without gmap4rails gem

Image
 To Integrate google maps in rails application without gem 1.) Go to the google maps https://developers.google.com/maps/web/ 2.) Click Get a key 3.) Go to the page where you want to show the google maps and add this tag <script src="https://maps.googleapis.com/maps/api/js?v=3&key=' YOURAPIKEY '&region=US" type="text/javascript"></script> 4.) Create a div for map            <div id="map-container" style='width: 360px; height: 400px;'>            <div id="map-canvas"></div>         </div> 5.) Now  what you need is latitude and longitude for the marker to show for that in rails you need to use a gem " Gecoder " ( https://github.com/alexreisner/geocoder ) 6.) In your Model here is School   geocoded_by :address   after_validation :geocode   attr_accessor :address, :latitude, :longitude   7.)  Follow the steps and get lat and long from the address of y

Completely Uninstall and Install Rubymine on ubuntu

To remove RubyMine-8.1 completely from your ubuntu take only three steps: 1.) rm -rf ~/.RubyMine80 2.) rm -rf /home/USERNAME/.gnome/apps/jetbrains-rubymine.desktop 3.) rm -rf /home/USERNAME/.local/share/applications/jetbrains-rubymine.desktop To install RubyMine-8.1 on ubuntu follow these 4 steps: 1.) Download rubymine-8.0.1.tar.gz 2.) Extract it and go to /RubyMIne-8.1/bin 3.) ./rubymine.sh 4.) Now it will open the rubymine now fill up your license and key if not go for evaluation for 30 days.

How to remove the bom from http response

What is bom? The byte order mark ( BOM ) is a Unicode character used to signal the endianness ( byte order ) of a text file or stream. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream. How to know the response is bom encoded or not: To know whether the response is bom encoded or not you just need to see the response if the response look something like this: BASE_URL = 'http://www.claritin.com/webservice/allergyforecast.php?zip=' resp = Net :: HTTP .get_response( URI ( " #{ BASE_URL }10007 " )).body "\xEF\xBB\xBF\"[{\\\"pollenForecast\\\":{\\\"zip\\\":\\\"10007\\\",\\\"city\\\":\\\"NEW YORK\\\",\\\"state\\\":\\\" NY\\\",\\\"forecast\\\":[1.1,0.1,0.8,0.6],\\\"pp\\\":\\\"Mixed Trace.\\\",\\\"timestamp\\\":\\\"11\\/14\\/2015 12:00:00 AM\\\"},\\\"weatherForecast\\\&qu

Valid Email Regex

I tried writing a perfect email regex which can be supported for almost every standard website like google, facebook etc. \A[a-zA-Z0-9]*[\.?*[a-zA-Z0-9]]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+){1,3}\z Please check on this regex if you find better please reply:

Why to use symbol rather than a string in ruby

Symbols are abstract references represented, typically, by a short string prefixed with a colon.   Examples include :blue , :good , and :name . Sadly, there is no succinct, easy-to-learn trick to symbols, so you’ll need to read this whole section—maybe even more than once—to get it to stick. Among mainstream languages, symbols are reasonably unique to Ruby (although Lisp and Erlang do have similar concepts) and tend to confuse most new users, so let’s jump straight into an illustrative example: current_situation = :good puts "Everything is fine" if current_situation == :good puts "PANIC!" if current_situation == :bad Everything is fine In this example, :good and :bad are symbols. Symbols don’t contain values or objects , like variables do. Instead, they’re used as a consistent name within code. For example, in the preceding code, you could easily replace the symbols with strings, like so: current_situation = "good" puts "Everything is fine&

How to find out all descendants of ActiveRecord::Base

To find out all descendants of ActiveRecord::Base you need to first eager load the Rails application. Rails.application.eager_load!  Then find out all the descendants models  ActiveRecord::Base.descendants To find out all descendants without STI ActiveRecord::Base.direct_descendants

Aws basic commands in ruby

First create an object of s3 client through loading yml of aws_credential which looks like: aws_credentails:   access_key_id: access_key_id   secret_access_key: secret_access_key   region: region_name   source_upload_bucket: bucket_name   rails_bucket: rails_bucket_name   ipad_bucket: ipad_bucket_name(if exists)   application_arn: if_exists name_prefix: prefix_name_of_bucket s3_client= Aws::S3::Client.new(aws_credentials) 1.) To list all buckets:  s3_client.list_buckets 2.) Listing objects of a particular bucket: s3_client.list_objects(bucket:"bucket-name") 3.) Listing all key of a bucket in an array: s3_client.list_objects({bucket: "bucket-name"})[:contents].map{|x| x.key} 4.) To delete a key from a bucket:  s3_client.delete_object(bucket: "bucket-name", key:"10_sheet.zip_256") 5.) To copy existing directory to new directory: def copy_existing_dir_to_new_dir(bucket_name=bucket-name)     responce = s3.list_o

Using recaptcha in rails without gem

Image
To use google recaptcha in your application you need to obtain a key value pair which includes public and private key    To get the the keys click here https://www.google.com/recaptcha/intro/index.html   It will redirect you to google reCAPTCHA home page -> Get reCAPTCHA -> fill the form where label is the label for your  site and domain is the domain name of your site. then you will get the keys site key and secret key .Now put this where you want the google recaptcha   <script src='https://www.google.com/recaptcha/api.js'></script> <div class="g-recaptcha" data-sitekey=" site-key "></div> Then you will get the button like this. Then clicking on the checkbox will open a default select image box:     This verify callback will give you the response which is handled with an ajax call in rails or it can be used in any kind of application. < script type= "text/javascript" >

OAuth2 Overview

Image
Introduction OAuth 2 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean. It works by delegating user authentication to the service that hosts the user account, and authorizing third-party applications to access the user account. OAuth 2 provides authorization flows for web and desktop applications, and mobile devices. This informational guide is geared towards application developers, and provides an overview of OAuth 2 roles, authorization grant types, use cases, and flows. Let's get started with OAuth Roles! OAuth Roles OAuth defines four roles: Resource Owner Client Resource Server Authorization Server We will detail each role in the following subsections. Resource Owner: User The resource owner is the user who authorizes an application to access their account. The application's access to the user's account is limited to the

Adding new Methods to your association for advanced queries

You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example: class Customer < ActiveRecord :: Base has_many :orders do   def find_customer_by_order_id ( order_id ) o = Order . find ( order_id ) o .customer end   end end now in console: >>c=Customer.first  #<Customer id: 1, name: "ttttttttttt", created_at: "2015-04-29 12:30:13", updated_at: "2015-05-05 10:36:06", orders_count: 1>  >>c.orders.find_customer_by_order_id(2)  #<Customer id: 1, name: "ttttttttttt", created_at: "2015-04-29 12:30:13", updated_at: "2015-05-05 10:36:06", orders_count: 1>   If you have an extension that should be shared by many associations, you can use a named extension module. For example: module FindR

When to choose between has_and_belongs_to_many and has_many, through association

Image
This was really confusing for me when to choose has_and_belongs_to_many and when to choose has_many , through association because both sets up many-to-many relationship between tables. Qns:  When to choose has_and_belongs_to_many association . Ans: The answer is when we does not need any kind of information about the association or the information about join model then we should use has_and_belongs_to_many association. Example: An assembly can have many parts and a part can have many assemblies and we don't need any other information about association then sets up direct many-to-many association between tables. via another table called assemblies_parts and it should have the foreign keys for both the table. Qns: When to choose has_many, through association.   Ans: The answer is when we need the information about the association or the information about join model then we should use has_many ,through association. Example: A physician can have many

Exploring the :inverse_of option on rails model association

 Let's start with the basic models and defining inverse of relationship in these models: class Criminal < ActiveRecord::Base   belongs_to :prison, inverse_of: :criminals end class Prison < ActiveRecord::Base   has_many :criminals, inverse_of: :prison end  Memory Optimization When Fetching Associated Records It turns out that associated objects do not point to the same in-memory objects by default. To illustrate: prison   = Prison.create(name: 'Bad House') criminal = prison.criminals.create(name: 'Krazy 8')   # Without :inverse_of criminal.prison == prison # Prison Load (0.1ms)  SELECT "prisons".* FROM "prisons" WHERE "prisons"."id" = 2 LIMIT 1 # => true   # With :inverse_of criminal.prison == prison # => true When we call criminal.prison without :inverse_of on both the :belongs_to and :has_many associations, it will hit the database. With :inverse_of , if we already have that prison record