Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitue #to_s for Dashboard#title_attribute #5

Merged
merged 1 commit into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class CustomerDashboard
def title_attribute
:name
end

def attribute_adapters
{
email: :email,
Expand All @@ -16,7 +12,7 @@ def index_page_attributes
end

def show_page_attributes
attributes - [title_attribute]
attributes - [:name]
end

def form_attributes
Expand Down
4 changes: 0 additions & 4 deletions app/dashboards/order_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ def show_page_attributes
attributes
end

def title_attribute
:id
end

private

def attributes
Expand Down
4 changes: 0 additions & 4 deletions app/dashboards/product_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def show_page_attributes
attributes
end

def title_attribute
:name
end

private

def attributes
Expand Down
4 changes: 4 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class Customer < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true

def to_s
name
end

def lifetime_value
"$#{rand(100)}"
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class Order < ActiveRecord::Base
validates :address_city, presence: true
validates :address_state, presence: true
validates :address_zip, presence: true

def to_s
id
end
end
4 changes: 4 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Product < ActiveRecord::Base
validates :image_url, presence: true
validates :name, presence: true
validates :price, presence: true

def to_s
name
end
end
6 changes: 5 additions & 1 deletion app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<table>
<thead>
<tr>
<th><%= @presenter.resource_name.titleize %></th>

<% @presenter.attribute_names.each do |attr_name| %>
<th><%= attr_name %></th>
<th><%= attr_name.to_s.titleize %></th>
<% end %>
<th colspan="2"></th>
</tr>
Expand All @@ -16,6 +18,8 @@
<tbody>
<% @resources.each do |resource| %>
<tr>
<td><%= link_to resource.to_s, @presenter.show_path(resource) %></td>

<% @presenter.attribute_names.each do |attr_name| %>
<td><%= @presenter.render_attribute(resource, attr_name) %></td>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/presenters/form_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def attribute_names
end

def page_title
resource.public_send(dashboard.title_attribute)
resource.to_s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach - super clean! 👍

end

def index_path
Expand Down
14 changes: 5 additions & 9 deletions lib/presenters/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def new_path
end

def render_attribute(resource, attribute_name)
if attribute_name == dashboard.title_attribute
link_to attribute_html(resource, attribute_name), show_path(resource)
else
attribute_html(resource, attribute_name)
end
attribute_html(resource, attribute_name)
end

def show_path(resource)
route(nil, resource_name, resource)
end

protected
Expand All @@ -34,8 +34,4 @@ def render_attribute(resource, attribute_name)
def attribute_html(resource, attribute_name)
adapter(dashboard, resource, attribute_name).render_index
end

def show_path(resource)
route(nil, resource_name, resource)
end
end
2 changes: 1 addition & 1 deletion lib/presenters/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(dashboard, resource)
end

def page_title
resource.public_send(dashboard.title_attribute)
resource.to_s
end

def attributes
Expand Down
8 changes: 0 additions & 8 deletions spec/dashboards/customer_dashboard_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
require "rails_helper"

RSpec.describe CustomerDashboard do
describe "#title_attribute" do
it "is the name" do
dashboard = CustomerDashboard.new

expect(dashboard.title_attribute).to eq(:name)
end
end

describe "#index_page_attributes" do
it "includes the name and email" do
dashboard = CustomerDashboard.new
Expand Down