Skip to content

Commit

Permalink
Merge pull request #306 from platanus/toggle-builder-bug
Browse files Browse the repository at this point in the history
fix(toggle-bool-builder): show if update action is enabled only
  • Loading branch information
ldlsegovia authored May 29, 2020
2 parents 4797342 + 98eca52 commit fe59a0a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/activeadmin_addons/addons/toggle_bool_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ToggleBoolBuilder < CustomBuilder
def render
raise ArgumentError, 'Block should not be used in toggle bool columns' if block
return if conditional_eval_hide?

context.div class: 'toggle-bool-switches-container' do
context.span toggle
end
Expand All @@ -12,6 +13,7 @@ def toggle
toggle_classes = 'toggle-bool-switch'
toggle_classes += ' on' if data
toggle_classes += ' notify-success' if options[:success_message]
return unless enabled_controller_action?(:update)

context.span(
'',
Expand All @@ -21,7 +23,7 @@ def toggle
'data-object_id' => model.id,
'data-field' => attribute,
'data-value' => data,
'data-url' => context.auto_url_for(model),
'data-url' => resource_url,
'data-success_message' => options[:success_message]
)
end
Expand All @@ -30,6 +32,7 @@ def conditional_eval_hide?
[:if, :unless].any? do |cond|
if options[cond]
raise ArgumentError, "'#{cond}' option should be a proc" unless options[cond].is_a?(Proc)

result = options[cond].call(model)
cond == :if ? !result : result
end
Expand Down
20 changes: 20 additions & 0 deletions lib/activeadmin_addons/support/custom_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ def self.builder_method_name

protected

def resource_url
admin_resource&.route_instance_path(model, {})
end

def enabled_controller_action?(action)
admin_controller_actions.include?(action)
end

def admin_controller_actions
(admin_controller&.action_methods&.to_a || []).map(&:to_sym)
end

def admin_controller
context&.controller
end

def admin_resource
context.active_admin_resource_for(model.class)
end

def data
@data ||= block ? block.call(model) : model.send(attribute)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/features/toggle_bool_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@
end
end
end

context "with disabled update action" do
before do
register_page(Invoice) do
actions :index, :show

index do
toggle_bool_column :active
end
end

@invoice = create_invoice(active: true)
visit admin_invoices_path
end

it { expect(page).not_to have_css("#toggle-invoice-#{@invoice.id}-active") }
end
end

0 comments on commit fe59a0a

Please sign in to comment.