Skip to content
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ Add the **RedAlert** gem to your Gemfile.
end
```

You can pass in symbols or strings and we'll build the buttons for you:

```ruby
rmq.app.alert title: "Hey!", actions: [ "Go ahead", :cancel, :delete ] do |button_tag|
case button_tag
when :cancel then puts "Canceled!"
when :delete then puts "Deleted!"
when "Go ahead" then puts "Going ahead!"
end
end
```


You can even use the `make_button` helper to create custom UIAction buttons to add:
```ruby
# Use custom UIAction buttons and add them
Expand Down
10 changes: 5 additions & 5 deletions lib/project/button_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ def self.add_template_actions(template, &block)
[
rmq.app.make_button(title: yes, tag: :yes, &block),
rmq.app.make_button(title: no, tag: :no, &block),
rmq.app.make_button({title: cancel, tag: :cancel, style: :cancel}, &block)
rmq.app.make_button(title: cancel, tag: :cancel, style: :cancel, &block)
]
when :ok_cancel
[
rmq.app.make_button(ok, &block),
rmq.app.make_button({title: cancel, tag: :cancel, style: :cancel}, &block)
rmq.app.make_button(title: ok, &block),
rmq.app.make_button(title: cancel, tag: :cancel, style: :cancel, &block)
]
when :delete_cancel
[
rmq.app.make_button({title: delete, tag: :delete, style: :destructive}, &block),
rmq.app.make_button({title: cancel, tag: :cancel, style: :cancel}, &block)
rmq.app.make_button(title: delete, tag: :delete, style: :destructive, &block),
rmq.app.make_button(title: cancel, tag: :cancel, style: :cancel, &block)
]
else
[]
Expand Down
27 changes: 26 additions & 1 deletion lib/project/red_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def alert(opts={}, &block)
actions = []
if opts[:actions] && opts[:actions].is_a?(Array) && opts[:actions].length > 0
# caller has pre-defined actions
actions << opts[:actions]
actions << map_action_buttons(opts[:actions], &block)
elsif opts[:actions] && opts[:actions].is_a?(Symbol)
# caller wants a template
actions << add_template_actions(opts[:actions], &block)
Expand Down Expand Up @@ -77,6 +77,31 @@ def make_button(opts={}, &block)
AlertAction.new(opts, &block)
end

# Returns an array of action buttons based on the symbols you pass in.
# Usage Example:
# rmq.app.alert title: "Hey!", actions: [ "Go ahead", :cancel, :delete ] do |button_tag|
# puts "#{button_text} pressed"
# end
def map_action_buttons(buttons, &block)
yes = NSLocalizedString("Yes", nil)
no = NSLocalizedString("No", nil)
cancel = NSLocalizedString("Cancel", nil)
ok = NSLocalizedString("OK", nil)
delete = NSLocalizedString("Delete", nil)

buttons.map do |button|
case button
when :cancel then make_button(title: cancel, tag: :cancel, style: :cancel, &block)
when :yes then make_button(title: yes, tag: :yes, &block)
when :no then make_button(title: no, tag: :no, &block)
when :ok then make_button(title: ok, tag: :ok, &block)
when :delete then make_button(title: delete, tag: :delete, style: :destructive, &block)
when String then make_button(title: button, tag: button, &block)
else button
end
end
end

end # close eigenclass

end # close App
Expand Down
88 changes: 88 additions & 0 deletions spec/map_buttons_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
describe "RedAlert.map_action_buttons" do

it "builds cancel button" do
test_array = [ :cancel ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "Cancel"
subject[0].tag.should == :cancel
subject[0].style.should == :cancel
subject[0].handler.should == callback
end

it "builds delete button" do
test_array = [ :delete ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "Delete"
subject[0].tag.should == :delete
subject[0].style.should == :destructive
subject[0].handler.should == callback
end

it "builds ok button" do
test_array = [ :ok ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "OK"
subject[0].tag.should == :ok
subject[0].style.should == :default
subject[0].handler.should == callback
end

it "builds yes button" do
test_array = [ :yes ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "Yes"
subject[0].tag.should == :yes
subject[0].style.should == :default
subject[0].handler.should == callback
end

it "builds no button" do
test_array = [ :no ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "No"
subject[0].tag.should == :no
subject[0].style.should == :default
subject[0].handler.should == callback
end


it "builds custom button" do
test_array = [ "Custom" ]
callback = ->(){}
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "Custom"
subject[0].tag.should == "Custom"
subject[0].style.should == :default
subject[0].handler.should == callback
end

it "builds lots of buttons" do
callback = ->(){}
custom_button = RubyMotionQuery::AlertAction.new(title: "Custom", tag: "Custom", &callback)
test_array = [ "A", "B", "C", custom_button, :yes, :no, :ok, :delete, :cancel ]
subject = RubyMotionQuery::App.map_action_buttons(test_array, &callback )
subject.should.be.all {|s| s.is_a?(RubyMotionQuery::AlertAction) }
subject[0].title.should == "A"
subject[1].title.should == "B"
subject[2].title.should == "C"
subject[3].should.be == custom_button
subject[4].title.should == "Yes"
subject[5].title.should == "No"
subject[6].title.should == "OK"
subject[7].title.should == "Delete"
subject[8].title.should == "Cancel"
end


end