# Accepting arguments for method defined by add\_to\_class

**URL:** https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184
**Category:** Development
**Created:** [September 10, 2019, 3:10pm UTC](https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184 "2019-09-10T15:10:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![markvanlan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markvanlan/32/160087_2.png) [@markvanlan](https://meta.discourse.org/u/markvanlan)
#### Post date: [September 10, 2019, 3:10pm UTC](https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184/1 "2019-09-10T15:10:20Z")

</div>

Instance methods can be added to classes by plugins using `add_to_class`, defined as:

```plaintext
def add_to_class(class_name, attr, &block)
  reloadable_patch do |plugin|
    klass = class_name.to_s.classify.constantize rescue class_name.to_s.constantize
    hidden_method_name = :"#{attr}_without_enable_check"
    klass.public_send(:define_method, hidden_method_name, &block)

    klass.public_send(:define_method, attr) do |*args|
      public_send(hidden_method_name, *args) if plugin.enabled?
    end
  end
end

```

Is there an existing way to do something like this, where the defined method takes arguments?

```plaintext
add_to_class(Guardian, :ensure_user_can_access_report, group, report) do
  ...
end

```

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 10, 2019, 3:15pm UTC](https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184/2 "2019-09-10T15:15:44Z")

</div>

> [@Restrict themes to certain groups?](https://meta.discourse.org/t/restrict-themes-to-certain-groups/87868/6):
>
> As a starting point, you could have a look at how theme access is overridden for the [theme-creator.discourse.org](http://theme-creator.discourse.org) system: The allow\_theme? method is used in Discourse’s application controller here: As Sam said, what you want to do is possible, but it will require a fair amount of development work to get it working smoothly

This seems like what you’re looking for. Makes for a good example.

So, basically, you’ll have to make those params as params to the lambda itself.

---

<div class="post-metadata">

### Author: ![markvanlan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markvanlan/32/160087_2.png) [@markvanlan](https://meta.discourse.org/u/markvanlan)
#### Post date: [September 10, 2019, 3:24pm UTC](https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184/3 "2019-09-10T15:24:51Z")

</div>

Yeah, thats exactly it. Thanks!

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [September 10, 2019, 3:31pm UTC](https://meta.discourse.org/t/accepting-arguments-for-method-defined-by-add-to-class/128184/4 "2019-09-10T15:31:07Z")

</div>

You can do the same thing the ruby way too.
