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/

Friday, June 28, 2013

Hosting A (static) Web Site On S3, GitHub Pages

S3:
http://blogging.alastair.is/how-i-served-100k-users-without-crashing-and-only-spent-0-32/
Of course, you can’t run any dynamic scripting on S3, but you’d be surprised the number of times you don’t have to. For example, the promo site for my taxi appdoes use dynamic content- the map tile images are generated by an EC2 instance I have running TileStream- but the vast majority of the page runs quite happily on S3. JSONP or CORS mean that you can quite effectively run an ‘API’ server on an EC2 instance, while leaving the majority of your static HTML on an S3 bucket.

Github Pages:
http://pages.github.com/
https://learn.andrewmunsell.com/learn/jekyll-by-example

Wednesday, June 26, 2013

Facebook Graph API

https://graph.facebook.com/jessestay

https://graph.facebook.com/jessestay?metadata=1

https://graph.facebook.com/jessestay?fields=name,picture

https://graph.facebook.com/icentrisdevs?fields=name,picture,friends&oauth_token=425581657554925|jssQVK-ENgAFILVt5-XVQRSYbl4
(To get the oauth_token - go to Access Token tool - https://developers.facebook.com/tools/access_token/)

Javascript Test Console & Examples:
https://developers.facebook.com/tools/console/

Posting a photo to a Wall:
http://stackoverflow.com/questions/3361507/facebook-js-sdk-how-to-post-an-image-on-wall
(see HBK's answer)

Posting a Canvas element as a Photo to Facebook's Wall:
https://coderwall.com/p/4qpmfw
    -- https://github.com/lukasz-madon/heroesgenerator
        -- uses code from here - http://stackoverflow.com/questions/5292689/sending-images-from-canvas-elements-using-ajax-and-php-files

Phonegap & Facebook

Options:

1. FacebookConnect plugin for Phonegap Build
https://github.com/phonegap-build/FacebookConnect

2. Phonegap-Facebook-plugin
https://github.com/phonegap/phonegap-facebook-plugin

3. without using native plugin  (added on Jun 12 2013)
https://github.com/studiosoton/faceGap
Jquery PhoneGap Facebook Connect Plugin - without native plugin
This plugin connects to facebook without using native plugin (Android, iOS, etc)
Plugin working mode "Site Login with Facebook"

Sunday, June 23, 2013

Creating a new Phonegap app to work with in Eclipse

http://docs.phonegap.com/en/2.2.0/guide_getting-started_android_index.md.html

After Steps 1-3, for each project we have to repeat Step 4.

1. Navigate to where 'create.exe' is: 
C:\Windows\System32> cd C:\dev\phonegap-2.8.1\lib\android\bin

2. C:\dev\phonegap-2.8.1\lib\android\bin> create   C:\dev\PGAndroid2 com.psjplearn.PGAndroid2   PGAndroid2

C:\dev\PGAndroid2 = folder to create for new project
com.psjplearn.PGAndroid2 = package name (com.companyname.appname)
PGAndroid2 = project name

3. Add files to assets\www folder



Javascript - scope, closures

Sunday, June 16, 2013

Saturday, June 15, 2013

Backbone.js - 1

OOP using Classic Javascript vs Backbone.js:
JS Bin
Backbone Models: JS Bin

Frameworks vs Libraries, Backbone.js

From Pluralsight's course on Backbone.js
"Frameworks call your code , while your code calls Libraries...
Another way to think of it is - Frameworks control how you design you app...they specify things like, 'where' you put 'what' files, what you can do, and how you do it..
It's great for getting started, and it's great for getting things done quickly, but it is terrible for flexibility..
Libraries provide some useful features, and then you are on your own.
Backbone for example, provides help with a number of common user interface features, but does not help at all with how you design your application. It's both its strength and weakness." 
...
Backbone is a library of tools..

Running a .apk file in Android Emulator

From :
http://stackoverflow.com/questions/3480201/how-do-you-install-an-apk-file-in-the-android-emulator
http://stackoverflow.com/questions/4449540/failure-install-failed-already-exists-when-i-tried-to-update-my-application

Pre-requisite - install Java & ADT bundle from http://developer.android.com/sdk/index.html

Download the .apk file (from Phonegap Build)

In console - go to \sdk\platform-tools directory (It will have adb.exe)
C:\dev\adt-bundle-windows-x86_64-20130522\sdk\platform-tools> adb install -r PGApp1.apk
(-r --> required to re-install the same app)

Note: It is taking a really long time (several minutes) for the Emulator to start on Windows 7 64 bit machine (Have seen similar complaints in StackOverflow questions)

Thursday, June 13, 2013

Javascript at LinkedIn

HTML5 Canvas related

http://garage.socialisten.at/2012/12/new-adventures-in-html5-part-1-html5-canvas/
We briefly considered doing everything Canvas-related in this app in plain JavaScript, but since we wanted to use layers, events and transitions (all of which are not available in the Canvas API), we opted to use the Canvas framework KineticJS.
http://www.html5canvastutorials.com/

HTML5 Canvas - put 2 images beside each other

JS Bin
JS Bin
JS Bin
In the above code, toDataURL() is giving an error because of security restrictions. Observe the alert() does not fire.

Wednesday, June 12, 2013

Javascript - namespace/module example


Method1: (This is what I found in an example)
--------
var SILLY = (function (module) {
      
      module.DoIt = function (resultObject) {
          resultObject.prepend(Date() + '
'); }; return module; } (SILLY || {}); // "{}" means "new Object()". It is a shortcut NOTE: The last line could be : (SILLY || {}) => (SILLY || (new Object()))); ----- Method2: (This is how we would write the above code in C#/Java style) --------- var SILLY = (function() { if (SILLY === undefined) { var SILLY = new Object(); SILLY.DoIt = function (resultObject) { resultObject.prepend(Date() + '<br >'); }; } return SILLY; }());
jsbin link for full example - http://jsbin.com/ahuqow/1/edit

Phonegap learning - jQuery Mobile, Handlebars.js example

Saturday, June 8, 2013

Kendo UI & Phonegap

Mobile Development & Phonegap related

Javascript libraries:

From http://www.adobe.com/devnet/phonegap/articles/phonegap-apps-powered-by-developercom.html

Reuse Existing Tools

Since PhoneGap applications leverage Web technologies, there are countless existing libraries or frameworks that you can leverage to accelerate application development. Whether it is a solution accelerator framework like jQuery or Zepto, a UI toolkit like Twitter Bootstrap, jQuery Mobile, or Sencha, or a data visualization library like Raphael.js, Highcharts, or RGraph.js, there are lots of resources both open source and commercially that you can leverage within your applications.
Solution Accelerator Frameworks - jQuery, Zepto
UI Toolkits - Twitter Bootstrap, jQuery Mobile, Sencha
Data Visualization Libraries - Raphael.js, Highcharts, RGraph.js

3rd party Database options:

1. Parse.com -
http://www.adobe.com/devnet/phonegap/articles/using-parse-with-phonegap-a-marriage-made-in-awesome.html - Dec 2012

2. Database.com -
http://www.adobe.com/devnet/phonegap/articles/phonegap-apps-powered-by-developercom.html - June 2012

Push Notifications:

Below plugin allows the App to receive push notifications on both Android and iOS devices:
https://github.com/phonegap-build/PushPlugin
Tutorial - http://devgirl.org/2013/01/24/push-notifications-plugin-support-added-to-phonegap-build/

Above github link also mentions a Ruby Gem called "pushmeup"  (See section titled "Test Environment")
(Use - to push notifications directly from your desktop during development)

Phonegap:
 https://github.com/phonegap/phonegap/wiki

BEM methodology for UI organization

http://bem.info/method/definitions/
BEM Architecture is mentioned on Adobe's topcoat.io page.

Sublime Text shortcuts

Toggle SideBar - Ctrl K,B
Layout - Alt Shft 1 (or Alt Shft 2) 

Friday, June 7, 2013

Phonegap related

http://www.tricedesigns.com/2013/01/18/my-workflow-for-developing-phonegap-applications/
One other advantage of PhoneGap build is that it lets designers/developers build mobile applications without having to install any developer tools. If you want to compile a PhoneGap app for iOS, but are on Windows – just use PhoneGap build and you won’t need Xcode or a Mac.
Good beginner's tutorial for Phonegap & Phonegap Build - http://www.youtube.com/watch?v=ZgUAT7CIhsY
Need to try this tutorial by Christophe Coenraets - http://coenraets.org/blog/phonegap-tutorial/

Monday, June 3, 2013

Rails: Get currently logged in user details

On Dev machine - rails console - the below gives the details of the logged in user (i.e. myself)

User.find(:all, :conditions => ["current_sign_in_ip != '' "])

(Found it here - https://groups.google.com/forum/?fromgroups#!topic/plataformatec-devise/tOKU_jcWNp0 )

Sunday, June 2, 2013

console settings (for PYR project)

To see all the routes displayed by "rake routes" (on Windows console):

1) rake routes | grep #

2) The output of the above command is fairly long, so, to see the whole output, we have to change the console's properties to something similar to the below 


Wednesday, May 8, 2013

html5please.com

html5please.com - advices on polyfills

HTML5 Please is a community project from the people behind HTML5 Boilerplate, Modernizr & CSS3 Please.


Saturday, April 27, 2013

DBNotes app

An example project that I did as part of my Rails training -

Code on Github - https://github.com/theAcadian/DBNotes

Demo - http://screencast.com/t/UXaEGpEBduO

Github's code analysis shows that code is 70% Javascript, 30% Ruby.
UI was done using Bootstrap components.

Friday, April 12, 2013

git commands 1

To follow a project on GitHub (for e.g. my DBNotes project) - copy the repository and take updates when needed

1. To copy a repository from github.com -
     we have to 'clone' it
     Suppose we run the below command (observe that 'current directory' is C:\Sites)
     C:\Sites > git clone git@github.com:theAcadian/DBNotes.git

Result of above command will be to create a directory called "DBNotes"

2. To get updates -
     C:\Sites > cd DBNotes
   C:\Sites\DBNotes > git pull git@github.com:theAcadian/DBNotes.git

3. To run the project (these are Rails commands) -
    C:\Sites\DBNotes > bundle install
    C:\Sites\DBNotes > rake db:migrate
    C:\Sites\DBNotes > rails s

 4. Open localhost:3000 in browser.

Friday, March 29, 2013

ifttt

Yahoo Pipes, ifttt.com (If This Then That)

Friday, March 22, 2013

SignalR & Shopping Cart

Markdown - tool for webwriters

Markdown is a text-to-HTML conversion tool (for web writers).

http://en.wikipedia.org/wiki/Markdown

Dino Esposito on "conventions"

Dino Esposito on "conventions"

"Conventions when not well explained, understood, and known, are quite dangerous. They do not favor readable code and even may create more issues than they would solve in development scenarios where awareness of conventions is not high. Too many, and too deep conventions, look undistinguishable from magic. Personally, I find acceptable the level of conventions you find in ASP.NET MVC; much less the extreme level of conventions you find in Web API."
This is from his article comparing Web API vs ASP.Net MVC Controllers vs WCF - https://www.simple-talk.com/dotnet/.net-framework/building-a-public-http-api-for-data/

Saturday, March 16, 2013

Javascript code - Bookmarklet that loads jQuery and then executes some jQuery commands

RunSomejQueryCommands() - Put whatever jQuery statements that need to be run after jQuery finishes loading
Note: the code loads jQuery into the page only if it is Not already there
Below example - in google.com page, clicking on the bookmarklet will load jQuery and put the text "jQuery loaded...Inserted via jQuery" into the Search box
javascript:if (typeof jQuery == 'undefined') { var s=document.createElement('script');s.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);} var WaitTilljQueryLoads = function() { if (typeof jQuery != 'undefined') { clearInterval(timer);RunSomejQueryCommands(); }}; var timer = setInterval(WaitTilljQueryLoads);void(s);var RunSomejQueryCommands = function() {$('[name=q]').val('jQuery loaded...Inserted via jQuery');};

Saturday, March 9, 2013

Zen Coding (Plugins)

Plugins are available for Visual Studio, Notepad++ etc..

To use with Sublime Text - https://github.com/sergeche/emmet-sublime
(Github link has instructions on how to install and use it)


From http://en.wikipedia.org/wiki/Zen_Coding:

Zen Coding uses a specific syntax in order to expand small snippets of code, similar to CSS selectors, into full-fledged HTML code.[2] For example, the sequence
div#page>div.logo+ul#navigation>li*5>a
expands into
<div id="page">
        <div class="logo"></div>
        <ul id="navigation">
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
                <li><a href=""></a></li>
        </ul>
</div>
Intro by author - http://coding.smashingmagazine.com/2009/11/21/zen-coding-a-new-way-to-write-html-code/

EDIT (Mar 31 2013) -
Zen Coding is now Emmet - emmet.io
Documentation links -
http://docs.emmet.io/
Abbreviations Syntax - http://docs.emmet.io/abbreviations/syntax/
Actions - http://docs.emmet.io/actions/
Cheat Sheet - http://docs.emmet.io/cheat-sheet/

Wednesday, February 20, 2013

Mobile - HYBRID App-Building Tools (as of Dec 2012)

From the document - "Going Hybrid - How hybrid apps are accelerating HTML5 adoption" by BusinessInsider.com
  • PhoneGap
  • PhoneGap Build (Adobe)
  • Titanium (Appcelerator)
  • Kendo UI (Telerik)
  • Sencha

Tuesday, February 5, 2013

Real-Time Collaborative Editors


http://collabedit.com/97msj
http://piratepad.net/Arw934fJ2O
http://piratepad.net/ep/pad/view/Arw934fJ2O/latest

http://highscalability.com/

Followers

Blog Archive