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

More Solid Account Selector #66

Merged
merged 2 commits into from
May 25, 2013
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
11 changes: 10 additions & 1 deletion lib/sup/horizontal_selector.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Redwood

class HorizontalSelector
class UnknownValue < StandardError; end

attr_accessor :label, :changed_by_user

def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
Expand All @@ -13,7 +15,14 @@ def initialize label, vals, labels, base_color=:horizontal_selector_unselected_c
@changed_by_user = false
end

def set_to val; @selection = @vals.index(val) end
def set_to val
raise UnknownValue, val.inspect unless can_set_to? val
@selection = @vals.index(val)
end

def can_set_to? val
@vals.include? val
end

def val; @vals[@selection] end

Expand Down
11 changes: 8 additions & 3 deletions lib/sup/modes/edit_message_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ def initialize opts={}
HorizontalSelector.new "Account:", AccountManager.user_emails + [nil], user_emails_copy + ["Customized"]

if @header["From"] =~ /<?(\S+@(\S+?))>?$/
@account_selector.set_to $1
@account_user = ""
# TODO: this is ugly. might implement an AccountSelector and handle
# special cases more transparently.
account_from = @account_selector.can_set_to?($1) ? $1 : nil
@account_selector.set_to account_from
else
@account_selector.set_to nil
@account_user = @header["From"]
end

# A single source of truth might better than duplicating this in both
# @account_user and @account_selector.
@account_user = @header["From"]

add_selector @account_selector
end

Expand Down
40 changes: 40 additions & 0 deletions test/unit/test_horizontal_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "test_helper"

require "sup/horizontal_selector"

describe Redwood::HorizontalSelector do
let(:values) { %w[foo@example.com bar@example.com] }
let(:strange_value) { "strange@example.com" }

before do
@selector = Redwood::HorizontalSelector.new(
'Acc:', values, [])
end

it "init w/ the first value selected" do
first_value = values.first
@selector.val.must_equal first_value
end

it "stores value for selection" do
second_value = values[1]
@selector.set_to second_value
@selector.val.must_equal second_value
end

describe "for unknown value" do
it "cannot select unknown value" do
@selector.wont_be :can_set_to?, strange_value
end

it "refuses selecting unknown value" do
old_value = @selector.val

assert_raises Redwood::HorizontalSelector::UnknownValue do
@selector.set_to strange_value
end

@selector.val.must_equal old_value
end
end
end