How to Send a Parameter to a Before Filter

How can I send a parameter to a before filter?

You should be able to do this with a block:

before_filter {|controller| controller.check_role('admin') }

before_filter with parameters

I'd do it like this:

before_filter { |c| c.authenticate_rights correct_id_here }

def authenticate_rights(project_id)
project = Project.find(project_id)
redirect_to signin_path unless project.hidden
end

Where correct_id_here is the relevant id to access a Project.

How to pass variable value to .filter()

The easiest way would be simply to use an anonymous function and close over the variable you want to use:

var minSize = 10;
var filtered = [12, 5, 8, 130, 44].filter( val => val >= minSize );

If you really wanted to keep a named function to call back to, you could partially apply a parameter with bind:

function isBigEnough(minSize, value) {
return value >= minSize;
}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough.bind(null,10));

Here, bind will return a new function with the leftmost parameter bound to 10 (the first argument, the one that is null, is what gets passed as this to the function when being invoked). So the parameter that filter passes in will show up as the second value in the callback.

How do I pass an extra parameter to the callback function in Javascript .filter() method?

Make startsWith accept the word to compare against and return a function which will then be used as filter/callback function:

function startsWith(wordToCompare) {
return function(element) {
return element.indexOf(wordToCompare) === 0;
}
}

addressBook.filter(startsWith(wordToCompare));

Another option would be to use Function.prototype.bind [MDN] (only available in browser supporting ECMAScript 5, follow a link for a shim for older browsers) and "fix" the first argument:

function startsWith(wordToCompare, element) {
return element.indexOf(wordToCompare) === 0;
}

addressBook.filter(startsWith.bind(this, wordToCompare));

I dont really understand how the default parameters it takes are passed

There is nothing special about it. At some point, filter just calls the callback and passes the current element of the array. So it's a function calling another function, in this case the callback you pass as argument.

Here is an example of a similar function:

function filter(array, callback) {
var result = [];
for(var i = 0, l = array.length; i < l; i++) {
if(callback(array[i])) { // here callback is called with the current element
result.push(array[i]);
}
}
return result;
}

Passing arguments to filters - best practices

You have to use procs for this.

class FooController < ApplicationController
before_filter { |controller| controller.send(:generic_filter, "XYZ") },
:only => :edit
before_filter { |controller| controller.send(:generic_filter, "ABC") },
:only => :new

private
def generic_filter type
end
end

Edit

One more way to pass the parameter is to override the call method of ActionController::Filters::BeforeFilter.

class ActionController::Filters::BeforeFilter
def call(controller, &block)
super controller, *(options[:para] || []), block
if controller.__send__(:performed?)
controller.__send__(:halt_filter_chain, method, :rendered_or_redirected)
end
end
end

Now you can change your before_filter specification as follows

class FooController < ApplicationController

# calls the generic_filter with param1= "foo"
before_filter :generic_filter, :para => "foo", :only => :new

# calls the generic_filter with param1= "foo" and param2="tan"
before_filter :generic_filter, :para => ["foo", "tan"], , :only => :edit

private
def generic_filter para1, para2="bar"
end
end

How to pass variable to filter function within a R function

so I assume you want to filter by target good or bad.
In my understanding, always filter() before you group_by(), as you will possibly ommit your filter variables. I restructured your function a little:

    dset <- data.frame(target, var_1)
odds_by_var <- function(dataframe, variable, target_var, target_val){

df_name <- paste('odds', deparse(substitute(variable)), sep = "_")
variable_string <- deparse(substitute(variable))
target_string <- deparse(substitute(target_var))

temp_df1 <- dataframe %>%
group_by_(variable_string, target_string) %>%
summarise(cnt = n()) %>%
mutate(total = sum(cnt),
rate = cnt / total)
names(temp_df1) <- c(variable_string,"target","cnt","total","rate" )
temp_df1 <- temp_df1[temp_df1$target == target_val,]
assign( df_name,temp_df1, envir=.GlobalEnv)

}

odds_by_var(dset, var_1, target, "bad")

result:

> odds_var_1
# A tibble: 2 x 5
# Groups: var_1 [2]
var_1 target cnt total rate
<chr> <chr> <int> <int> <dbl>
1 debit_order bad 1 4 0.25
2 other bad 1 2 0.5

How to pass parameter to my custom filters

First of all probably you want to override OnActionExecuted event.

To pass parameter you have to create constructor with parameter:

private paramType param;

public AuditAttribute(paramType param)
{
this.param = param;
}

Before filter, calling a method with param throws syntax error. Ruby

:check_user_validity is a symbol which is something like a string. You can't "run" a symbol with parentheses. You are effectively doing something like 'function_name'(...) which is invalid syntax.

before_filter or before_action works by passing it a function name (by using symbols) or function (by using proc/lambda) to be called later when a request is received.



Related Topics



Leave a reply



Submit