Skip to content

Instantly share code, notes, and snippets.

View flarnie's full-sized avatar

Flarnie Marchan flarnie

View GitHub Profile
@flarnie
flarnie / Rails Nested Form Example
Created August 15, 2013 04:18
A very basic example of a nested form.
<form action="<%= posts_url %>" method="post">
<label for="title">Title</label>
<input type="text" name="post[title]" value="<%= @post.title %>">
<br />
<label for="link_1">Link 1</label>
<input type="text" name="post[links_attributes][0][url]">>
<br />
<label for="link_2">Link 2</label>
<input type="text" name="post[links_attributes][1][url]">
<br />
@flarnie
flarnie / self.comments_by_parent(link)
Created August 17, 2013 13:49
Recursive Partial Example part I- helper method to create comments-by-parent hash.
@flarnie
flarnie / View of top level comments
Created August 17, 2013 13:52
Recursive Partial Example Part II- Rendering the top level comments in the view.
<ul>
<% @comments_by_parent[nil].each do |comment| %>
<%= render "comment", :comment => comment, :comments_by_parent => @comments_by_parent %>
</ul>
<% end %>
@flarnie
flarnie / Recursive Partial Example
Created August 17, 2013 13:53
Recursive Partial Example Part III- the recursive partial
<li><p><%= comment.body %>
</p>
</li>
<h4>Comment ON A COMMENT</h4>
<p class="teeny">so meta</p>
<%= render "new_comment", :link => comment.link, :parent_comment_id => comment.id %>
<% if comments_by_parent[comment.id] != [] %>
<ul>
<% comments_by_parent[comment.id].each do |subcomment| %>
<%= render "comment", :comment => subcomment, :comments_by_parent => comments_by_parent %>
@flarnie
flarnie / Validate Email in RoR
Created August 17, 2013 14:14
The right way to validate uniqueness case insensitive of email in Ruby on Rails.
##Check uniqueness of email-----------------------------------------------
#in migration
add_index :users, :email, :unique => true
#but that doesn't cover case insensitive
#in the model take email to lowercase before save
class User..
attr_accessible :email
#this gives you custom message for the error
@flarnie
flarnie / 'inherits' JS helper
Created August 22, 2013 15:47
A helper function to get a target class to inherit from another class without side-effecting the parent class prototype.
Function.prototype.inherits = function(parent){
function Surrogate(){};
Surrogate.prototype = parent.prototype;
this.prototype = new Surrogate();
}
@flarnie
flarnie / rspec_ready.rb
Created October 20, 2013 14:07
A template to set up RSpec and FactoryGirl for testing when generating a new Rails app. Use: rails new my_app_name_here -m rspec_ready.rb
gem_group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
gem_group :test do
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'launchy'
@flarnie
flarnie / index.html
Last active December 27, 2015 00:59
CSS3 Media Queries Demo
<html>
<head>
<link rel="stylesheet" type="text/css" href="media_qs.css" >
</head>
<body>
<b>HELLO WORLD.</b>
</body>
</html>
@flarnie
flarnie / todo_mvc_demo_spec.rb
Last active August 29, 2015 13:57
A demo feature spec that will run on the ToDoVMC:Backbone.js Demo if capybara and selenium are correctly set up.
require 'spec_helper'
describe "ToDoMVC:Backbone.js" do
# the following is a demo spec for demonstration purposes
# it is meant to run on the ToDoMVC:Backbone.js Demo app:
# http://todomvc.com/architecture-examples/backbone/
it "allows creation of a todo-list item", :js => true do
visit "http://todomvc.com/architecture-examples/backbone/"
fill_in "new-todo", with: "TODOITEM Hello World\n"
@flarnie
flarnie / webpack.config.js
Last active January 8, 2016 05:30
Webpack configuration file for Rails setup
/**
* @see http://webpack.github.io/docs/configuration.html
* for webpack configuration options
*/
module.exports = {
// 'context' sets the directory where webpack looks for module files you list in
// your 'require' statements
context: __dirname + '/app/assets/javascripts',
// 'entry' specifies the entry point, where webpack starts reading all