Skip to content

Instantly share code, notes, and snippets.

@maxim
Last active January 24, 2025 17:28
Show Gist options
  • Save maxim/1e5c6816e97ecbe7e15eb3d364b0c9ea to your computer and use it in GitHub Desktop.
Save maxim/1e5c6816e97ecbe7e15eb3d364b0c9ea to your computer and use it in GitHub Desktop.

Revisions

  1. maxim revised this gist Jan 24, 2025. 2 changed files with 10 additions and 14 deletions.
    12 changes: 5 additions & 7 deletions 1-welcome_page.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,11 @@
    class WelcomePage
    attr_reader :greeting, :recent_activity, :support_email

    class << self
    def from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end
    def self.from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end

    def initialize(greeting:, recent_activity:, support_email:)
    12 changes: 5 additions & 7 deletions 4-welcome_page_portrayal.rb
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,11 @@
    # In this one I also do an example definition of ActivityItem class.

    class WelcomePage < ApplicationStruct
    class << self
    def from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end
    def self.from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end

    keyword :greeting
  2. maxim revised this gist Jan 24, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 3-view.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <h2><%= @page.greeting %></2>
    <h2><%= @page.greeting %></h2>

    <ul>
    <%= @page.recent_activity.each do |item| %>
  3. maxim revised this gist Jan 24, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions 3-view.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    <h2><%= @page.greeting %></2>

    <ul>
    <li>
    <%= @page.recent_activity.each do |item| %>
    <%= @page.recent_activity.each do |item| %>
    <li>
    <strong><%= item.name %></strong>: <%= item.description %>
    <% end %>
    </li>
    </li>
    <% end %>
    </ul>

    <p>Get help: <%= mail_to @page.support_email %></p>
  4. maxim created this gist Jan 24, 2025.
    18 changes: 18 additions & 0 deletions 1-welcome_page.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    class WelcomePage
    attr_reader :greeting, :recent_activity, :support_email

    class << self
    def from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end
    end

    def initialize(greeting:, recent_activity:, support_email:)
    @greeting = greeting
    @recent_activity = recent_activity
    @support_email = support_email
    end
    end
    3 changes: 3 additions & 0 deletions 2-controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    def show
    @page = WelcomePage.from_user(current_user)
    end
    11 changes: 11 additions & 0 deletions 3-view.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <h2><%= @page.greeting %></2>

    <ul>
    <li>
    <%= @page.recent_activity.each do |item| %>
    <strong><%= item.name %></strong>: <%= item.description %>
    <% end %>
    </li>
    </ul>

    <p>Get help: <%= mail_to @page.support_email %></p>
    27 changes: 27 additions & 0 deletions 4-welcome_page_portrayal.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # This is an alternative example of welcome_page.rb using https://github.com/maxim/portrayal
    # Assuming that ApplicationStruct has `extend Portrayal`.
    # In this one I also do an example definition of ActivityItem class.

    class WelcomePage < ApplicationStruct
    class << self
    def from_user(user)
    new \
    greeting: "Good #{user.time_of_day}, #{user.name}",
    recent_activity: ActivityItem.from_user(user),
    support_email: Rails.configuration.app.support_email
    end
    end

    keyword :greeting
    keyword :support_email
    keyword :recent_activity, defines: 'ActivityItem' do
    def self.from_user(user)
    user.events.last(3).map { |event|
    new(name: event.name, description: event.description)
    }
    end

    keyword :name
    keyword :description
    end
    end