Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
369 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
module ActiveAdmin | ||
class ControllerAction | ||
attr_reader :name | ||
attr_reader :name, :http_verb | ||
|
||
def initialize(name, options = {}) | ||
@name, @options = name, options | ||
@name = name.to_sym | ||
@http_verb = resolve_http_verb(options) | ||
end | ||
|
||
def remove_http_verbs(verbs) | ||
self.http_verb = http_verb - verbs | ||
end | ||
|
||
def http_verb | ||
@options[:method] ||= :get | ||
private | ||
|
||
attr_writer :http_verb | ||
|
||
def resolve_http_verb(options) | ||
method = options[:method] | ||
return [:get] unless method | ||
|
||
Array(method).map { |m| m.downcase.to_sym }.uniq | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module ActiveAdmin | ||
class Resource | ||
class ControllerActions | ||
def initialize | ||
@actions = [] | ||
end | ||
|
||
def items | ||
copy | ||
end | ||
|
||
# Adds a new action to the collection. | ||
# Don't allow add actions with the same name and http method, | ||
# in order to avoid routes duplication. | ||
def <<(new_action) | ||
actions.each do |action| | ||
next if action.name != new_action.name | ||
|
||
new_action.remove_http_verbs(action.http_verb) | ||
return if new_action.http_verb.blank? | ||
end | ||
|
||
actions << new_action | ||
end | ||
|
||
def each(&block) | ||
copy.each(&block) | ||
end | ||
|
||
def size | ||
actions.size | ||
end | ||
|
||
private | ||
|
||
attr_accessor :actions | ||
|
||
def copy | ||
actions.dup | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
lib/active_admin/resource_controller/action_verb_combiner.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module ActiveAdmin | ||
class ResourceController < BaseController | ||
module ActionVerbCombiner | ||
private | ||
|
||
# Add behaviour to an existing action, depending on http method, | ||
# when the same action name is specified. | ||
# | ||
# For example: | ||
# | ||
# ActiveAdmin.register Post do | ||
# member_action :comments, method: :get do | ||
# @post = Post.find(params[:id] | ||
# @comments = @post.comments | ||
# end | ||
# | ||
# member_action :comments, method: :post do | ||
# @post = Post.find(params[:id] | ||
# @post.comments.create!(comment_params) | ||
# end | ||
# end | ||
# | ||
# Will be equivalent to: | ||
# | ||
# ActiveAdmin.register Post do | ||
# member_action :comments, method: [:get, :post] do | ||
# if request.post? | ||
# @post = Post.find(params[:id] | ||
# @post.comments.create!(comment_params) | ||
# else | ||
# @post = Post.find(params[:id] | ||
# @comments = @post.comments | ||
# end | ||
# end | ||
# end | ||
# | ||
def add_behaviour_to_action(action_name, http_verbs, &block) | ||
old_method_name = add_alias_for_old_behaviour(action_name) | ||
new_method_name = add_method_for_new_behaviour(action_name, &block) | ||
|
||
define_method(action_name) do | ||
if request.method_symbol.in?(http_verbs) | ||
send new_method_name | ||
else | ||
send old_method_name | ||
end | ||
end | ||
end | ||
|
||
def add_alias_for_old_behaviour(action_name) | ||
method_name = resolve_method_name(action_name, "old") | ||
alias_method method_name, action_name | ||
|
||
private method_name | ||
|
||
method_name | ||
end | ||
|
||
def add_method_for_new_behaviour(action_name, &block) | ||
method_name = resolve_method_name(action_name, "new") | ||
define_method method_name, &block | ||
|
||
private method_name | ||
|
||
method_name | ||
end | ||
|
||
def resolve_method_name(action_name, postfix) | ||
next_name = "#{action_name}_#{postfix}" | ||
|
||
while private_method_defined?(next_name) || method_defined?(next_name) | ||
next_name = "#{next_name}_#{postfix}" | ||
end | ||
|
||
next_name | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.