Sunday, July 28, 2013

jquery - .prop() vs .attr()

http://api.jquery.com/prop/
http://stackoverflow.com/questions/5874652/prop-vs-attr/5884994#5884994
A DOM element is an object, a thing in memory. Like most objects in OOP, it has properties. It also, separately, has a map of the attributes defined on the element (usually coming from the markup that the browser read to create the element). Some of the element's properties get their initial values fromattributes with the same or similar names (value gets its initial value from the "value" attribute; hrefgets its initial value from the "href" attribute, but it's not exactly the same value; className from the "class" attribute). Other properties get their initial values in other ways: For instance, the parentNodeproperty gets its value based on what its parent element is; an element always has a style property, whether it has a "style" attribute or not.


Ruby - how to access properties using 'string' version of property-names

(2012 question/answer)
http://stackoverflow.com/questions/12136262/ruby-get-set-an-objects-property-using-a-string-symbol
car.color
car.send("color=", value)
(2009 question/answer)
http://stackoverflow.com/questions/903763/how-to-convert-from-a-string-to-object-attribute-name
irb> example_customer.name
#=> "Evagation Governessy"
irb> field = 'name'
#=> "name"
irb> example_customer.instance_variable_get(field)
NameError: `name` is not allowed as an instance variable name
from (irb):8:in `instance_variable_get`
from (irb):8
irb> example_customer.instance_variable_get('@'+field)
#=> nil
irb> example_customer.send(field)
#=> "Evagation Governessy"
irb> example_customer.send(field+'=', "Evagation Governessy Jr.")
#=> "Evagation Governessy Jr."
irb> example_customer.send(field)
#=> "Evagation Governessy Jr."
irb> example_customer.name
#=> "Evagation Governessy Jr."
So you can see how #send(field) accesses the record information, and trying to access the attributes doesn't. Also, we can use #send(field+'=') to change record information. 

Ruby - strings - difference between using single quotes & double quotes


https://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics
A String literal created with single quotes does not support interpolation.
The essential difference between using single or double quotes is that double quotes allow for escape sequences while single quotes do not. What you saw above is one such example. “\n” is interpreted as a new line and appears as a new line when rendered to the user, whereas '\n' displays the actual escape sequence to the user.
Example of interpolation:
a = 1
b = 4
puts "The number #{a} is less than #{b}" 

Thursday, July 25, 2013

Captcha related

Have to read this -
http://15daysofjquery.com/safer-contact-forms-without-captchas

Then this:
http://stackoverflow.com/questions/8472/practical-non-image-based-captcha-approaches

Have to see if we can use Raphael.js to draw captchas (Not sure if we can provide the same kind of functionality as reCaptcha or not i.e. audio etc)

Tuesday, July 23, 2013

Rails - newer property syntax

When using the newer property syntax, there should not be a space between the property-name and colon (:)

i.e.
class: 'icon-picture'     --> correct
class : 'icon-picture'    --> incorrect 

Observe the space before the colon (:) in the incorrect line

Rails - passing html5 'data' attributes to link_to

In the example in the previous post, notice the syntax that is used to specify key & value for data-toggle

2 ways:
(from http://stackoverflow.com/questions/2134702/ruby-1-9-hash-with-a-dash-in-a-key )

(I)"data-toggle" => 'modal'
<%= link_to " Change Image", "#modalDiv1", { class: 'icon-picture', "data-toggle" => 'modal' } %>

i.e.
1. use double quotes (i.e. string instead of symbol)
2. use hash-rocket syntax (=>) instead of colon (:) syntax

It seems we cannot use the new colon syntax with keys that contain a hyphen (-) as in data-toggle

(II)data: { toggle: 'modal' } 
<%= link_to " Change Image", "#modalDiv1", { class: 'icon-picture', data: { toggle:'modal'} }%>

i.e.
1. Specify a 'data' hash for 'data' attributes:

Rails - Bootstrap Modal Dialog (pre-loaded & ajax)

Thursday, July 18, 2013

Rails - class_name

When trying to establish relationship between 2 models, we have to specify :class_name if 'module' is different

/app/models/abc/address.rb
module ABC
class Address < ActiveRecord::Base
belongs_to :user
                attr_accessible :user_id, :address1, :address2, :city, :country, :state, :zip, :is_primary_address
         end
end

/app/models/user.rb
class User < ActiveRecord::Base
       has_many :addresses, :class_name => ABC::Address
       ...
       ...
end

Sublime Text - Color Schemes (with White background)

Eiffel
Mac Classic
Dawn

Sunday, July 14, 2013

Rails - Difference between attr_accessor and attr_accessible

http://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible
attr_accessor is a ruby method that makes a getter and a setter. attr_accessible is a Rails method that allows you to pass in values to a mass assignment: new(attrs) or up update_attributes(attrs).
Here's a mass assignment:
Order.new({ :type => 'Corn', :quantity => 6 })
You can imagine that the order might also have a discount code, say :price_off. If you don't tag :price_off as attr_accessible you stop malicious code from being able to do like so:
Order.new({ :type => 'Corn', :quantity => 6, :price_off => 30 })
Even if your form doesn't have a field for :price_off, if it's in your model it's available by default. This means a crafted POST could still set it. Using attr_accessible white lists those things that can be mass assigned.
http://stackoverflow.com/questions/4838399/how-i-can-set-attr-accessible-in-order-to-not-allow-access-to-any-of-the-field
By default the attributes are all attr_accessible (which means they can be set my mass-assignment).
  • attr_accessible - only this list of attributes can be set by mass-assignment (white-listing).
  • attr_protected - these attributes cannot be set by mass-assignment (black-listing).
  • attr_readonly - these attributes cannot be set except for when the record is created.
Beginning with Rails 3.1, the following configuration option is available to disable mass-assignment by default for all models until you explicitly call attr_accessible or attr_protected:
config.active_record.whitelist_attributes = true

Rails Routing - path_names

Using "path_names" :

http://localhost:3000/users/sign_up
=>
http://localhost:3000/users/register

Friday, July 12, 2013

Rails - customizing 'scaffold' generator

In 'lib' folder, create the following folders: templates > erb > scaffold

Create index.html.erb and other files here (i.e. in 'scaffold' folder)
(The default versions of the above files that come with 'rails' are here - for v3.2.13 - https://github.com/rails/rails/tree/v3.2.13/railties/lib/rails/generators/erb/scaffold/templates )

Railscast about Generators - http://railscasts.com/episodes/216-generators-in-rails-3?view=comments
(Watch from 8mins onwards for customizing info)

Other useful links related to Generators-
http://stackoverflow.com/questions/4333393/using-rails-generate-scaffold-when-model-already-exists?rq=1
http://stackoverflow.com/questions/4630911/override-default-scaffold-generator-in-rails-3
http://stackoverflow.com/questions/8688220/rails-3-1-changing-default-scaffold-views-and-template
http://guides.rubyonrails.org/generators.html

Rails - Javascript

From http://railsapps.github.io/rails-javascript-include-external.html

Rules of Thumb

In summary, here are rules of thumb to guide your use of JavaScript in Rails:
  • Logically organize your site-wide scripts in the app/assets/javascripts/ folder.
  • Copy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder.
  • Let the Rails asset pipeline combine them all in one minimized application.js file.
  • List scripts in the app/assets/javascripts/application.js manifest.
In almost all applications, there is no need to add external JavaScript libraries directly in a view template. Use the Rails asset pipeline, even for JavaScript used on just one page (page-specific JavaScript). Copy external scripts to your application and you’ll gain the performance benefits of the Rails asset pipeline and avoid complexity.

Rails - to see all .js files that are being loaded

add the following to the URL -    ?debug_assets=1 

See Railscast on Assets Pipeline for more info - http://railscasts.com/episodes/279-understanding-the-asset-pipeline

Monday, July 8, 2013

Linux commands

Removing a directory that is not empty:
rm -Rf directory-name

Sublime Text:
http://www.dilipprajapati.com/2012/12/22/sublime-text-2-as-default-editor-in-ubuntu-12-04/

Rails, RVM:
https://rvm.io/integration/gnome-terminal
http://ryanbigg.com/2010/12/ubuntu-ruby-rvm-rails-and-you/
http://stackoverflow.com/questions/9056008/installed-ruby-1-9-3-with-rvm-but-command-line-doesnt-show-ruby-v/9056395#9056395

Is MongoDB running:
http://stackoverflow.com/questions/5091624/is-mongodb-running
check with either:
   ps -edaf | grep mongo   # "ps" flags may differ on your OS
or
   service mongodb status
Useful rvm commands to manage ruby versions using rvm:
http://stackoverflow.com/questions/8868555/how-do-i-uninstall-ruby-and-gems-using-rvm
Basically taken from http://beginrescueend.com/rvm/:
To list the available ruby versions to install type:
rvm list known
To then install from the list of known, type:
rvm install VERSION_NUMBER
To then use the ruby version you have installed:
rvm use VERSION_NUMBER
You can make a certain ruby version your default system version:
rvm use VERSION_NUMBER --default
To remove the ruby version, keeping the gemsets:
rvm uninstall VERSION_NUMBER
To remove ruby et al:
rvm remove VERSION_NUMBER
To learn about your ruby environment and where they are installed/aliased:
rvm info

Saturday, July 6, 2013

Cross Platform Mobile development related

Martin Fowler's slides on mobile development - Native vs Web vs Hybrid -
http://martinfowler.com/articles/multiMobile/

Calatrava (from Thoughtworks - Martin Fowler's company) -
http://overwatering.org/blog/2012/10/announcing-calatrava/

Native CSS - NativeCSS.com - native apps, CSS styling

Phonegap - http://www.asyncdev.net/2012/10/phonegap-a-misunderstood-hybrid-framework/
 I often hear as well that PhoneGap is used to wrap a website into an app. Let's clear things up. PhoneGap does allow you to embed an HTML/JS/CSS "web page" into a native shell, but its real strength lies in its more advanced usage: as a hybrid framework!


Adding a Textbox (or other HTML controls) to a HTML5 Canvas element

http://stackoverflow.com/questions/5073351/adding-textbox-and-other-controls-to-an-html5-game

This could be used to - allow the user to input text inside a Canvas element. (But the Input control itself will probably not become part of the Canvas.. Have to check this...One workaround could be - before posting the Canvas, we could capture the text that user has inputted, draw a box in the Canvas at the exact same location, add the text to it..And ask the user if it looks ok.. (we could limit the number of characters that the user enters)

Android Emulator - Camera Error - "insert SD card before using camera"

http://stackoverflow.com/questions/10912718/insert-sd-card-before-using-the-camera-alert-android-emulator
"When you create an AVD it's possible to say whether to emulate SDCard or not (and of what size). Looks like you simply did not specify the SDCard presence. Edit your AVD settings and make sure you have explicitly requested for SDCard support."

Monday, July 1, 2013

Mobile 'Backend As a Service' providers

Backend As A Service (BAAS) providers -

Parse.com (acquired by Facebook)
Database.com (from Salesforce)
http://www.built.io/

Followers

Blog Archive