Skip to content

Instantly share code, notes, and snippets.

View tyok's full-sized avatar

Mohammad Satrio tyok

View GitHub Profile
@tyok
tyok / normal.rb
Last active November 1, 2017 09:01
Ruby method chain
sample_text = "foo bar http://github.com/akitaonrails/chainable_methods foo bar"
sample_link = URI.extract(sample_text).first
uri = URI.parse(sample_link)
response = HTTParty.get(uri)
doc = Nokogiri::HTML.parse(response)
title = doc.css("H1").text
@tyok
tyok / magical_validation.rb
Last active October 24, 2017 10:53
Dry Validation + ActiveModel::Errors + Class-based approach
module MagicalValidation
module Model
extend ActiveSupport::Concern
delegate :errors, :hints, :messages, to: :@result, prefix: :nested
attr_reader :result
def initialize(hash)
@result = self.class.schema.call(hash)
end
# Functions:
(square = ((x) -> x * x))
# Arrays:
(list = [1, 2, 3, 4, 5])
# Objects:
(math = (
root: Math.sqrt
square: square
@tyok
tyok / pages_controller.rb
Last active August 29, 2015 14:04
Easy static pages on Rails
# Create an empty controller
class PagesController < ApplicationController
end
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@tyok
tyok / rspec_custom_matcher_capybara.rb
Created November 28, 2011 08:12 — forked from yosemsweet/rspec_custom_matcher_capybara.rb
Capybara matchers in RSpec custom matcher
RSpec::Matchers.define :have_widget_elements_for do |widget|
match do |actual|
puts actual.to_yaml
@errors = {}
expected_elements = {
:widget => ".widget",
:content_type => ".#{widget.content_type}",
:body => ".body",
:title => ".title",