This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using this to host an image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseView < SimpleDelegator | |
def self.collection(objects) | |
objects.map { |obj| new obj } | |
end | |
end | |
class UserView < BaseView | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ViewHelpers | |
extend ActiveSupport::Concern | |
included do | |
def default_url_options | |
ActionMailer::Base.default_url_options | |
end | |
end | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ViewHelpers | |
def urls | |
Rails.application.routes.url_helpers | |
end | |
def helpers | |
ActionController::Base.helpers | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserView < SimpleDelegator | |
def initialize(delegate, salutation: 'Padawan') | |
super(delegate) | |
@salutation = salutation | |
end | |
def fullname | |
"#{salutation} #{first_name} #{last_name}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < PersistentDataStore::Base | |
attr_accessor :first_name, :last_name | |
end | |
class UserView < SimpleDelegator | |
def fullname | |
"#{first_name} #{last_name}" | |
end | |
end |
NewerOlder