Friday, January 27, 2017

How to display the selected date in the edit form in Rails?

Use the selected: option in date_select to specify what date must be displayed in the edit form.

<%= f.date_select :published_at, order: [:month, :day, :year], start_year: Date.current.year - 5, end_year: Date.current.year + 1, selected: @episode.published_at %>

If you don't use this, when you edit the record next time, you will inadvertently change the date when you change some other field.

Friday, January 20, 2017

unable to obtain stable firefox connection in 60 seconds selenium webdriver

bundle update selenium-webdriver

The latest version of this gem has lot of problems. The solution was to downgrade the Firefox browser to 47.0.1. I found the workaround by reading the Capybara gem readme file. Yes, that's why they call me a genius.

Custom View Helpers in Rails 5

Learn how custom view helpers work in Rails 5. You will also learn how to read the Rails source code to create experiments that answers questions about how Rails works. We will use ack tool to search for exact matches in Rails source very fast.

Monday, January 16, 2017

Credit card declined : This customer has no attached payment source.

I was getting this error when using Stripe to subscribe a customer. Solution:

def allowed_params
  params.permit(:plan_name, :email, :password, :stripeToken, :authenticity_token)
end

customer = Stripe::Customer.create(email: email, plan: plan_id, source: stripe_token)

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => false %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => false %>

Best thing to do is to fail early and loudly, this will prevent the invalid request being submitted to Stripe. You must have:

raise 'Missing stripe token. Cannot process payment.' if params[:stripeToken].blank?                                        

Sunday, January 15, 2017

Rails Screencasts

Checkout the Rails screencast Autocomplete in Rails 5 : https://rubyplus.com/episodes/51
It covers using typeahead.js.

Checkout the Rails screencast Auto Complete Association in Rails 5 : https://rubyplus.com/episodes/181
It covers using jQuery for autocomplete feature.

Checkout the Rails screencast Movie Review Rails 5 App : https://rubyplus.com/episodes/1
It covers using Twitter Bootstrap 4

Checkout the Rails screencast Movie Review Rails 5 App : https://rubyplus.com/episodes/1
It covers using Elasticsearch for full text search

Checkout the Rails screencast Movie Review Rails 5 App - Part 3 : https://rubyplus.com/episodes/11
This covers devise.

Saturday, January 14, 2017

Simple Search Form in Rails 5

Learn how to use a simple search form to search the database for records that has the given string in one of the columns of a table. Watch the Rails screencast Episode #30

Thursday, December 29, 2016

Concept Diagram

A Concept Diagram is a graphic organizer that clarifies central concepts in a reading selection and in relating similar or associated information to this key idea. The steps to build a Concept Diagram:

Convey the central idea.
Offer the overall concept.
Note any key words.
Classify characteristics.
Explore examples and non-examples.
Practice with new examples.
Tie down the definition.

This sequence of steps provides a process for organizing and interpreting the content of a reading selection.

Steps to a Concept Diagram:

  1. Select a reading text for discussion.
  2. Have students identify the overall theme of the document and the major subthemes or concepts that together make up the document.
  3. Ask students to write down all key words in the selection and then group these terms in logical categories.
  4. Encourage students to suggest examples and non-examples of the key terms and concepts identified.
  5. Have the students produce a final, formal definition of key words and concepts by combining the things they learned when categorizing the words and providing examples.

References

1. Reading Educator
2. Lenski, Susan D., Wham, Mary Ann, & Johns, Jerry L. (1999). Reading and learning strategies for middle and high school students. Dubuque, IA: Kendall/Hunt.

RubyPlus Podcast Episode 21


require dlopen rvm ruby rails


/Users/bparanj/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/completion.rb:10:in `require': dlopen(/Users/bparanj/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/x86_64-darwin13/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib (LoadError)

Use:

group :development, :test do
  gem 'rb-readline'
end

and install the gem. Now the rails console will work.

GraphQL Basics in Rails 5

Checkout the GraphQL Basics in Rails 5 screencast

This screencast covers the basics of GraphQL. Demonstrates a simple hello GraphQL program using graphql gem and exposing a person has many friends nested ActiveRecord relationship as an API using graphql. The Rails engine graphiql-rails is used to play with the backend API.

Tuesday, December 27, 2016

Sunday, December 25, 2016

Orthogonality in Coding

Orthogonality is one of the most important properties that can help make even complex designs compact. In a purely orthogonal design, operations do not have side effects; each action (whether it's an API call, a macro invocation, or a language operation) changes just one thing without affecting others. There is one and only one way to change each property of whatever system you are controlling.
- The Art of UNIX Programming
Orthogonality means that features can be used in any combination, that the combinations all make sense, and that the meaning of a given feature is consistent, regardless of the other features with which it is combined.
- Programming Language Pragmatics
An orthogonal language is one in which you can express a lot by combining a small number of operators in a lot of different ways.
- On Lisp

The Principles of Modular and Maintainable Design by Jens Dietrich

Orthogonality is a concept often used to describe modular and maintainable software. The concept of orthogonality is based on the Greek word orthogōnios, meaning "right-angled." It is often used to express the independence between different dimensions. When an object moves along the x-axis in a three-dimensional space, its y and z coordinates don't change. Change in one dimension does not cause change in another dimension, which means that one dimension cannot cause side-effects for others.

This explains why the concept of orthogonality is often used to describe modular and maintainable software design: thinking about systems as points in a multi-dimensional space (spawned by independent, orthogonal dimensions) helps software developers to ensure that our changes to one aspect of system will not have side-effects for another.

Orthogonality is a powerful concept because it enables us to establish a relatively simple mental model for complex application use cases. In particular, we can focus on one dimension while ignoring other aspects.

Testing is a common scenario where orthogonality is useful. We can test the functionality of log levels using a suitable fixed pair of an appender and a layout. Orthogonality ensures us that there will be no surprises: log levels will work the same way with any given combination of appender and layout. Not only is this convenient (there is less work to do) but it is also necessary, because it would be impossible to test log levels with every known combination of appender and layout. This is especially true given that Log4j, like many software tools and utilities, is designed to be extended by third parties.

Designing and coding for orthogonality

 The key idea is to use abstraction. Each dimension of an orthogonal system addresses one particular aspect of the program. Such a dimension will usually be represented by a class. Each of these classes represent a dimension, while the objects represents the points within the given dimension.

Pending Work: This code here:  Monads in Recurring Payment Handling seriously needs some orthogonal love.

References

 Flattening Arrow Code
 Arrow Anti Pattern
 Java Tip: Orthogonality by example by Jens Dietrich