Skip to content

Instantly share code, notes, and snippets.

View sighmin's full-sized avatar
💛
grateful

Simon van Dyk sighmin

💛
grateful
View GitHub Profile
@sighmin
sighmin / Profile image hosting hack
Created April 28, 2022 16:48
My profile image
# Using this to host an image
# More ridiculousness here: https://github.com/chancancode/javascript
require "javascript"
puts "This is totally Ruby"
javascript {
console.log("ZOMG JavaScript");
var a = 1;
console.log(a);
var b = function(x) {
@sighmin
sighmin / extend_self.rb
Created February 2, 2018 20:42
extend self
# A friend of mine Stuart (https://github.com/stuartc) and I discussed this today.
# I think `extend self` adds the classes to the eigenclass of the module, or the eigenmodule? Whatever.
# My code used to look like
class BusinessBusiness
def initialize(foo, bar)
@foo = foo
@bar = bar
end
class BaseView < SimpleDelegator
def self.collection(objects)
objects.map { |obj| new obj }
end
end
class UserView < BaseView
...
end
def decorate(object, decorater = nil)
if object.is_a? Array
return [] if object.empty?
decorater_class = decorater || (object[0].class.name.split("::").last + "View").constantize
object.map { |o| decorater_class.new(o) }
else
decorater_class = decorater || (object.class.name + "View").constantize
decorater_class.new(object)
end
end
class DashboardView
def initialize(current_user, users, featured_posts)
@current_user = current_user
@users = users.map { |u| UserView.new(u) }
@featured_posts = featured_posts
end
attr_reader :current_user, :users, :featured_posts
end
# Instantiate this in your controller layer and pass it to the view as the only "source of truth" for the data on the page
module ViewHelpers
extend ActiveSupport::Concern
included do
def default_url_options
ActionMailer::Base.default_url_options
end
end
...
end
module ViewHelpers
def urls
Rails.application.routes.url_helpers
end
def helpers
ActionController::Base.helpers
end
end
class UserView < SimpleDelegator
def initialize(delegate, salutation: 'Padawan')
super(delegate)
@salutation = salutation
end
def fullname
"#{salutation} #{first_name} #{last_name}"
end
class User < PersistentDataStore::Base
attr_accessor :first_name, :last_name
end
class UserView < SimpleDelegator
def fullname
"#{first_name} #{last_name}"
end
end