Skip to content

Instantly share code, notes, and snippets.

View m-Peter's full-sized avatar
⛩️
Zanshin

Ardit Marku m-Peter

⛩️
Zanshin
  • Athens, Greece
View GitHub Profile
@m-Peter
m-Peter / active_form_test.rb
Created August 9, 2014 14:18
class_eval issue
require 'test_helper'
class ActiveFormTest < ActiveSupport::TestCase
class Base
attr_reader :model, :nested_units
def initialize(model)
@model = model
@nested_units = []
populate_units
@m-Peter
m-Peter / project_form.rb
Created June 12, 2014 10:41
A Form Model with a nested level of two.
class ProjectForm < AbstractForm
attribute :name
association :tasks, records: 3 do
attribute :name
association :sub_tasks, records: 3 do
attribute :name
validates :name, presence: true
@m-Peter
m-Peter / _form.html.erb
Last active August 29, 2015 14:02
A ProjectForm Model to create a Project along with its Tasks nested model in a single form submission.
<%= form_for @project_form do |f| %>
<% if @project_form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project_form.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project_form.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
@m-Peter
m-Peter / _email.html.erb
Last active August 29, 2015 14:02
A UserForm Model to create a User along with its Email and Profile nested models in a single form submission.
<h2>Email details</h2>
<%= f.fields_for :email, @user_form.email do |email| %>
<div class="field">
<%= email.label :address %><br>
<%= email.text_field :address %>
</div>
<% end %>
@m-Peter
m-Peter / new-after.html.erb
Created May 6, 2014 16:49
A rough prototype of how we would like Form Objects to work. This is an effort to abstract out operations from a context-specific Form Object.
<h1>Sign Up</h1>
<%= form_for @signup_form do |f| %>
<% if @signup_form.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% @signup_form.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
@m-Peter
m-Peter / bench_tr_and_gsub.rb
Created February 21, 2014 09:17
Benchmark the performance of String#tr and String#gsub . These two methods are both used to remove the non-numeric characters from a string.
require 'benchmark'
def strip_num_tr(number)
number.tr('^0-9', '')
end
def strip_num_gsub(number)
number.gsub(/\D/, '')
end