[英]ROR: wrong number of arguments (given 2, expected 1)
On the following piece of code在下面的一段代码上
def initialize(clause)
clause =~ /^([\w\_\.]+)(->'\w+')?_(desc|asc)$|^([\w\_\.]+->>'[\w\_]+')(->'\w+')?_(desc|asc)$/
@column = $1 || $4
@op = $2 || $5
@order = $3 || $6
@field = [@column, @op].compact.join
end
I am getting the following error我收到以下错误
ArgumentError (wrong number of arguments (given 2, expected 1)):
config/initializers/order_clause.rb:5:in `initialize'
Does anyone knows how to resolve it?有谁知道如何解决它? Please Help!
请帮忙!
EDIT - I am adding the full code for more clarification.编辑- 我正在添加完整的代码以进行更多说明。
module ActiveAdmin
class OrderClause
attr_reader :field, :order
def initialize(clause)
clause =~ /^([\w\_\.]+)(->'\w+')?_(desc|asc)$|^([\w\_\.]+->>'[\w\_]+')(->'\w+')?_(desc|asc)$/
@column = $1 || $4
@op = $2 || $5
@order = $3 || $6
@field = [@column, @op].compact.join
end
def valid?
@field.present? && @order.present?
end
def to_sql(active_admin_config)
table = column_in_table?(active_admin_config.resource_column_names, @column) ? active_admin_config.resource_table_name : nil
if json_column?(@column)
table_column = (@column =~ /\./) ? @column : [table, @column].compact.join(".")
['(', table_column, @op, ' ',')::numeric ', @order].compact.join
else
table_column = (@column =~ /\./) ? @column : [table, active_admin_config.resource_quoted_column_name(@column)].compact.join(".")
[table_column, @op, ' ', @order].compact.join
end
end
private
def json_column?(column)
column.include?('->>')
end
def column_in_table?(names, column)
column = json_column?(column) ? column.split('->>')[0].strip : column
names.include?(column)
end
end
end
The activeadmin
gem instantiates the ActiveAdmin::OrderClause
class with a couple of arguments ( active_admin_config
and order
) as you can see here and here .如您在此处和此处所见,
activeadmin
gem 使用几个 arguments( active_admin_config
和order
)实例化ActiveAdmin::OrderClause
class。 Fix the initialize
method arguments.修复
initialize
方法 arguments。
def initialize(active_admin_config, clause)
# ...
end
You should also remove the active_admin_config
argument from the to_sql
method since it is called with no arguments.您还应该从
to_sql
方法中删除active_admin_config
参数,因为它是在没有 arguments 的情况下调用的。 You can set @active_admin_config
in the initialize
method and add :active_admin_config
to the attr_reader
call to use in the to_sql
method.您可以在
initialize
方法中设置@active_admin_config
并将:active_admin_config
添加到attr_reader
调用以在to_sql
方法中使用。
module ActiveAdmin
class OrderClause
attr_reader :field, :order, :active_admin_config
def initialize(active_admin_config, clause)
@active_admin_config = active_admin_config
# rest of the code
end
def to_sql
# ...
end
end
end
I would recommend you create a CustomOrderClause
class that inherits from the gem's ActiveAdmin::OrderClause
class and only override the necessary methods.我建议您创建一个继承自 gem 的
ActiveAdmin::OrderClause
CustomOrderClause
的 CustomOrderClause class 并仅覆盖必要的方法。 You can then use config.order_clause = CustomOrderClause
when configuring activeadmin in an initializer.然后,您可以在初始化程序中配置 activeadmin 时使用
config.order_clause = CustomOrderClause
。
You are trying to override ActiveAdmin::OrderClause
I think and if you see ActivAdmin's code you will find this:我认为您正在尝试覆盖
ActiveAdmin::OrderClause
,如果您看到 ActivAdmin 的代码,您会发现:
module ActiveAdmin
class OrderClause
attr_reader :field, :order, :active_admin_config
def initialize(active_admin_config, clause)
clause =~ /^([\w\.]+)(->'\w+')?_(desc|asc)$/
@column = $1
@op = $2
@order = $3
@active_admin_config = active_admin_config
@field = [@column, @op].compact.join
end
This means wherever it is initialized it would be initialized with two arguments because their constructor accepts that.这意味着无论在哪里初始化它都会用两个 arguments 进行初始化,因为它们的构造函数接受它。 So you will have to change the
initialize
method in your code to accept two arguments or pass only one argument from wherever it is being called which would be tedious to do.因此,您必须更改代码中的
initialize
方法以接受两个 arguments 或仅从调用它的任何位置传递一个参数,这将是乏味的。
Rather than overwriting the entire class you should be able to use Module#prepend
to just alter the functionality you are interested in altering without having to worry about the rest of the implementation.而不是覆盖整个 class 您应该能够使用
Module#prepend
来更改您有兴趣更改的功能,而不必担心实现的 rest。 eg例如
module OrderClauseExtension
def initialize(active_admin_config, clause)
super
if field.empty?
clause =~ /^([\w\_\.]+->>'[\w\_]+')(->'\w+')?_(desc|asc)$/
@column = $1
@op = $2
@order = $3
@field = [@column, @op].compact.join
end
end
end
ActiveAdmin::OrderClause.prepend(OrderClauseExtension)
This will try the original implementation first and use your implementation as a fallback in the event that the field
attribute results in an empty String
which would be the case in [nil,nil].compact.join #=> ""
这将首先尝试原始实现,并在
field
属性导致空String
的情况下使用您的实现作为后备,这将是[nil,nil].compact.join #=> ""
中的情况
All the rest of the functionality contained in this class will remain unchanged and you will not have to worry about any changes to the gem itself (unless the initialize method and/or attributes assigned here change)此 class 中包含的功能的所有 rest 将保持不变,您不必担心 gem 本身的任何更改(除非初始化方法和/或此处分配的属性发生更改)
You can place this file in config/initializers
.您可以将此文件放在
config/initializers
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.