# Custom class attributes problem

**URL:** <https://www.ruby-forum.com/t/custom-class-attributes-problem/178986>\
**Category:** Rails\
**Created:** [November 14, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/custom-class-attributes-problem/178986 "2009-11-14T23:50:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [November 14, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/custom-class-attributes-problem/178986/1 "2009-11-14T23:50:26Z")

</div>

Hi.

Im creating a CRUD-extention for my ActionController. I am trying to  
make a class (inside the Crudable-module) that contains the options set  
for the extension. Like so:

```auto
  module Crudable
    # Extends the controller
    def self.included(controller)
      controller.helper_method( :render_form, :get_path_for,
:get_field_type )
      controller.extend(ClassMethods)
    end

    module ClassMethods # nodoc;
      def acts_crudable(options={})
        CrudOptions.new(self, options)
        self.send :include, ActionController::Crudable::InstanceMethods
      end
    end

    module InstanceMethods # nodoc;
      # All methods goes here...
    end
  end
```

Where the CrudOptions is the class that contains the options like  
prefix, special view-paths etc. This class looks like this:

```auto
  class CrudOptions
    @options = {}
    def self.new(klass, options={})
      @options = {}
      options[:prefix] = nil unless options[:prefix]
      @options = options
    end

    def self.options(index)
      @options[index]
    end
  end
```

But I’m betting there is a smarter way to create that CrudOption-class.  
But I can’t seem to get the hang of it. The problem is that the  
CrudOption-class keeps the options set from the first call to the  
new-method across new calls to the new-method. So calling the  
acts\_crudable(:prefix =\> ‘test’) and then calling acts\_crudable() will  
still keep the prefix ‘test’. How do I get around this.

If anyone can help me either fix the problem within the structure that  
is there now, or can rewrite some of the code I would really appreciate  
it!

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [November 15, 2009, 12:42am UTC](https://www.ruby-forum.com/t/custom-class-attributes-problem/178986/2 "2009-11-15T00:42:51Z")

</div>

Emil K. wrote:

> Hi.
> 
> Im creating a CRUD-extention for my ActionController  
> […]

Before you do this yourself, check out make\_resourceful.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
