Tuesday, May 6, 2014

h method Rails

Class Public methods

h(s)
Alias for: html_escape

html_escape(s) 
A utility method for escaping HTML tag characters. This method is also aliased as h.

In your ERB templates, use this method to escape any unsafe content. For example:
<%=h @person.name %>

puts html_escape('is a > 0 & a < 10?')
# => is a &gt; 0 &amp; a &lt; 10?

Also aliased as: h

html_escape_once(s)
A utility method for escaping HTML without affecting existing escaped entities.
html_escape_once('1 < 2 &amp; 3')
# => "1 &lt; 2 &amp; 3"

html_escape_once('&lt;&lt; Accept & Checkout')
# => "&lt;&lt; Accept &amp; Checkout"


http://www.webbydude.com/posts/9-the-h-helper-in-rails-3

The h() helper in Rails 3

How to escape/unescape HTML tags in Rails 3

Written by: David Zhu on November 06, 2010 22:35

Back in Rails 2.x, the h() helper escapes HTML input from the user, and renders it out as plain text.

For example, if I render out:

<%= @post.body %>


and the user has inputted HTML tags in the body field, like so:

        Hello <strong>World!</strong>

It will render out as:

        Hello World!

However, by adding the h() helper like so:

<%= h(@post.body) %>


it will escape all HTML and render out as

        Hello <strong>World!</strong>

just like what the user wrote. That's all good, but it's different in Rails 3.

Rails 3

In Rails 3, the h() helper is automatically appended, or in other words, it automatically escapes all code tags. There is no need to write h() anymore, it does it automatically.

However, what if you don't want to escape any HTML? The raw() helper does that:

<%= raw(@post.body) %>


By adding the raw() helper, all HTML and code tags that the user submits will be analyzed through the browser as real code. So by writing

        Hello <strong>World!</strong>

it will actually render out

        Hello World!

just as if you did not include the h() back in Rails 2.

Friday, May 2, 2014

What is a DSL - Domain Specific Language

Now I realize that there is a lot easier explanation of a DSL than I had posted below a year ago.  
I like to explain a DSL as being like Pig-Latin.

You can use Pig-Latin on English or French or Spanish or really many languages.
The main idea of using Pig-Latin is that you have to have a base language to use it on.
A DSL abstracts the base language for a different purpose, whether making it a bit easier to write HTML like Markdown or HAML both do or by using the base language for a specific purpose like writing tests (RSpec.)
A DSL is indeed a "mini-language" as stated below but specifically it is a "mini-language" which uses another language to write it.

Try some Pig-Latin 
For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, "ay" is added, as in the following examples:
  • "pig" → "igpay"
  • "banana" → "ananabay"
  • "trash" → "ashtray"
  • "happy" → "appyhay"
  • "duck" → "uckday"
  • "glove" → "oveglay"
Try some Markdown

"Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML). " ~ Daring Fireball

Markdown makes it quicker and easier for you to write HTML in your applications but it is actually converted to HTML when the browser reads your code.
  • <h1>This is First Level Header in HTML.</h1>
  • # This is a First Level Header in Markdown. (one # sign) 
  • <h2> This is a Second Level Header in HTML.</h2>
  • ## This is Second Level Header in Markdown. (two # signs) 
  • <blockquote>This is a blockquote in HTML.</blockquote>
  • < This is a blockquote in Markdown. (One < sign.)



My previous thoughts: 

We were talking about DSLs at my study group this week and it finally locked into my head what a DSL is. I didn't realize that RSpec was basically ruby with extra functionality so RSpec is a DSL. And now reading this wiki post, that HTML is a DSL and YAML and HAML. Wow.

http://en.wikipedia.org/wiki/Domain-specific_language
Domain-specific language 
A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains, and lacks specialized features for a particular domain. There are a wide variety of DSLs, ranging from widely used languages for common domains, such as HTML for web pages, down to languages used by only a single piece of software. 
DSLs can be further subdivided by the kind of language, and include domain-specific markup languages, domain-specific modeling languages (more generally, specification languages), and domain-specific programming languages. Special-purpose computer languages have always existed in the computer age, but the term "domain-specific language" has become more popular due to the rise of domain-specific modeling. Simpler DSLs, particularly ones used by a single application, are sometimes informally called mini-languages.

Wednesday, April 30, 2014

Instance Variables

http://www.railstutorial.org/book/rails_flavored_ruby#sec-a_user_class
In Rails, the principal importance of instance variables is that they are automatically available in the views, but in general they are used for variables that need to be available throughout a Ruby class.

Instance variables always begin with an @ sign, and are nil when undefined.

initialize, is special in Ruby: it’s the method called when we execute User.new.
https://www.ruby-lang.org/en/documentation/quickstart/2/ 
class Greeter
  def initialize(name = "World")
    @name = name
  end
  def say_hi
    puts "Hi #{@name}!"
  end
  def say_bye
    puts "Bye #{@name}, come back soon."
  end
end 
@name is an instance variable, and is available to all the methods of the class.

http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/110-instance-variables


http://www.rubyist.net/~slagell/ruby/instancevars.html
An instance variable has a name beginning with @, and its scope is confined to whatever object self refers to. Two different objects, even if they belong to the same class, are allowed to have different values for their instance variables. From outside the object, instance variables cannot be altered or even observed (i.e., ruby's instance variables are never public) except by whatever methods are explicitly provided by the programmer. As with globals, instance variables have the nil value until they are initialized. 
Instance variables do not need to be declared. This indicates a flexible object structure; in fact, each instance variable is dynamically appended to an object when it is first assigned.

***http://rubyhacking.andhapp.co.uk/2011/01/30/chapter-1.html 

Monday, April 28, 2014

RVM

Assumed knowledge
  • Don't type in the <> Just put whatever you want to use in there like the particular Rails version.
  • Don't type in the $ This means type it in your terminal.

$ rvm list - lists rubies
    rvm rubies

     ruby-1.9.3-p545 [ x86_64 ]
     ruby-2.0.0-p451 [ x86_64 ]
     =* ruby-2.1.1 [ x86_64 ]
     # => - current    # =* - current && default    # * - default

$ rvm use ruby 2.1.0-p547  (p means patch)

$ rvm gemset list
   gemsets for ruby-2.1.1 (found in /home/user/.rvm/gems/ruby-2.1.1)
   (default)
   global
L
  rvm gemset use <rails 4.0>

rvm gemset create <name> - (for example: name = rails -v 4.0.4)
L
  rvm gemset use

rvm install
L
  rvm use <ruby>

Thursday, April 24, 2014

Yield

http://www.ruby-doc.org/core-2.1.1/Fiber.html
Fiber
Fibers are primitives for implementing light weight cooperative concurrency in Ruby. Basically they are a means of creating code blocks that can be paused and resumed, much like threads. The main difference is that they are never preempted and that the scheduling must be done by the programmer and not the VM.
As opposed to other stackless light weight concurrency models, each fiber comes with a small 4KB stack. This enables the fiber to be paused from deeply nested function calls within the fiber block.
When a fiber is created it will not run automatically. Rather it must be be explicitly asked to run using the Fiber#resume method. The code running inside the fiber can give up control by calling Fiber.yield in which case it yields control back to caller (the caller of the Fiber#resume).
Upon yielding or termination the Fiber returns the value of the last executed expression
yield(args, ...) → obj
Yields control back to the context that resumed the fiber, passing along any arguments that were passed to it. The fiber will resume processing at this point when resume is called next. Any arguments passed to the next resume will be the value that this Fiber.yield expression evaluates to.

http://stackoverflow.com/questions/3066703/blocks-and-yields-in-ruby

http://www.tutorialspoint.com/ruby/ruby_blocks.htm

Presenter Object

http://robots.thoughtbot.com/post/13641910701/ti dy-views-and-beyond-with-decorators

http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters
   I hate not understanding a joke: Dijkstra's Algorithm

 

http://railscasts.com/episodes/287-presenters-from-scratch

http://stackoverflow.com/questions/20503445/using-presenters-ryan-bates-style-without-an-associated-model

http://eewang.github.io/blog/2013/09/26/presenting-the-rails-presenter-pattern/

Presenter is a Design Pattern
http://pivotallabs.com/presenter-sanity/
http://stackoverflow.com/questions/7860301/rails-patterns-decorator-vs-presenter

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter
Model–view–presenter (MVP) is a derivative of the model–view–controller (MVC) architectural pattern, also used mostly for building user interfaces.
In MVP the presenter assumes the functionality of the "middle-man" (played by the controller in MVC, although it must not be assumed that the presenter and controller play the same role). In MVP, all presentation logic is pushed to the presenter. Eventually, the model becomes strictly a domain model. 
(The domain model is a conceptual model of all the topics related to a specific problem. It describes the various entities, their attributes, roles, and relationships, plus the constraints that govern the problem domain.) http://en.wikipedia.org/wiki/Domain_model

Presenter and Decorator in Rails Slideshare

http://www.slideshare.net/thaichor/presenter-and-decorator-in-rails

Now to take a break with a Bundler Core Team favorite, Bee and PuupyCat!



Wednesday, April 23, 2014

Chapter 4 Crafting Rails 4 Applications

I am stuck just trying to run rails plugin new handlers

First of all there is a typo on page 64 There should NOT be a dash in between plug and in

I did a lot of rvm management and now I am getting a openssl error
$ dpkg -l | grep openssl

Dave suggested that I  check out:
https://groups.google.com/forum/#!topic/laruby-books/C1u-wZAYvHw
This didn't work

Cynthia suggested I install rvm

Interesting links people mentioned:
http://designinstruct.com/tool/responsive-html-email-framework-zurb-ink/

http://prose.io/

Rails Conf
http://www.railsconf.com/