Skip to content

Commit

Permalink
Add dialog: true option to form block
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Nov 8, 2017
1 parent 1951f3c commit 3e09f18
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/helpers/trestle/url_helper.rb
Expand Up @@ -14,7 +14,9 @@ def admin_link_to(content, instance=nil, options={}, &block)
end

def admin_url_for(instance, options={})
admin = options.key?(:admin) ? Trestle.lookup(options[:admin]) : admin_for(instance)
admin = Trestle.lookup(options[:admin]) if options.key?(:admin)
admin ||= admin_for(instance)

admin.path(options[:action] || :show, id: admin.to_param(instance)) if admin
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/trestle/resource/index.html.erb
@@ -1,7 +1,7 @@
<% content_for(:title, t("admin.titles.index", default: "Listing %{pluralized_model_name}", model_name: admin.model_name.titleize, pluralized_model_name: admin.model_name.plural.titleize)) %>
<% content_for(:primary_toolbar) do %>
<%= link_to admin.path(:new), class: "btn btn-default btn-lg" do %>
<%= link_to admin.path(:new), class: "btn btn-default btn-lg", data: ({ behavior: "dialog" } if admin.form.dialog?) do %>
<%= icon("fa fa-plus") %>
<span class="sr-only"><%= t("admin.buttons.new", default: "New %{model_name}", model_name: admin.model_name) %></span>
<% end unless admin.readonly? %>
Expand Down
4 changes: 2 additions & 2 deletions lib/trestle/admin/builder.rb
Expand Up @@ -42,8 +42,8 @@ def table(options={}, &block)
admin.table = Table::Builder.build(options.reverse_merge(admin: admin, sortable: true), &block)
end

def form(&block)
admin.form = Form.new(&block)
def form(options={}, &block)
admin.form = Form.new(options, &block)
end

def admin(&block)
Expand Down
10 changes: 7 additions & 3 deletions lib/trestle/form.rb
Expand Up @@ -8,10 +8,14 @@ class Form
autoload :Fields
autoload :Renderer

attr_reader :block
attr_reader :options, :block

def initialize(&block)
@block = block
def initialize(options={}, &block)
@options, @block = options, block
end

def dialog?
options[:dialog] == true
end

def render(template, instance)
Expand Down
2 changes: 1 addition & 1 deletion lib/trestle/form/automatic.rb
Expand Up @@ -2,7 +2,7 @@ module Trestle
class Form
class Automatic < Form
def initialize(admin)
@block = Proc.new do |instance|
super() do |instance|
admin.default_form_attributes.each do |attribute|
if attribute.array?
if [:string, :text].include?(attribute.type)
Expand Down
8 changes: 7 additions & 1 deletion lib/trestle/table/row.rb
Expand Up @@ -21,9 +21,15 @@ def initialize(row, template)

def options(instance)
options = Trestle::Options.new
options.merge!(data: { url: admin_url_for(instance) }) if table.admin

if table.admin
options.merge!(data: { url: admin_url_for(instance) })
options.merge!(data: { behavior: "dialog" }) if table.admin.form.dialog?
end

options.merge!(@row.options)
options.merge!(@template.instance_exec(instance, &@row.block)) if @row.block

options
end

Expand Down
3 changes: 2 additions & 1 deletion spec/trestle/admin/builder_spec.rb
Expand Up @@ -118,10 +118,11 @@ module TestHelper; end
b = Proc.new {}

Trestle::Admin::Builder.build(:test) do
form &b
form custom: "option", &b
end

expect(::TestAdmin.form).to be_a(Trestle::Form)
expect(::TestAdmin.form.options).to eq(custom: "option")
expect(::TestAdmin.form.block).to eq(b)
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/trestle/form_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Trestle::Form do
subject(:form) { Trestle::Form.new }

context "without options[:dialog]" do
it { is_expected.to_not be_dialog }
end

context "with options[:dialog]" do
subject(:form) { Trestle::Form.new(dialog: true) }

it { is_expected.to be_dialog }
end
end

0 comments on commit 3e09f18

Please sign in to comment.