Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there ActiveModel support? #2

Closed
chip opened this issue Nov 30, 2011 · 7 comments · Fixed by #30
Closed

Is there ActiveModel support? #2

chip opened this issue Nov 30, 2011 · 7 comments · Fixed by #30

Comments

@chip
Copy link

chip commented Nov 30, 2011

Hi,

Great work on your gem. Your docs referenced that it supports ActiveModel, but I didn't see any examples provided on how to do this. Here I have a simple ActiveModel class:


require 'active_model'

class ProductSearch
  include ActiveModel::Validations

  validates_length_of :keyword, :minimum => 1, \
                      :message => "must be at least 1 character."
  attr_accessor :keyword
  auto_strip_attributes :keyword

  def results
    puts "results keyword=#{@keyword}"
    @results ||= Solr.search(@keyword)
  end
end

Here's what I put in my Gemfile:

gem "auto_strip_attributes", "~> 2.0.2"

But when I fire up the Rails console, I get this:

ruby-1.8.7-p352 :003 > p = ProductSearch.new
NoMethodError: undefined method `auto_strip_attributes' for ProductSearch:Class
    from /Users/chip/code/rentwb/app/models/products/product_search.rb:17
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:454:in `load'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:454:in `load_file'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:593:in `new_constants_in'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:453:in `load_file'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:487:in `load_missing_constant'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:183:in `const_missing'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:181:in `each'
    from /Users/chip/code/rentwb/vendor/bundle/ruby/1.8/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:181:in `const_missing'
    from (irb):3

I've verified that this gem is installed here:

vendor/bundle/ruby/1.8/gems/auto_strip_attributes-1.1/lib/auto_strip_attributes.rb

Would you be able to provide an example of how to get this to work with ActiveModel? If I figure this out, I'll submit a Pull Request with an example. Thanks for your help!

Chip

@holli
Copy link
Owner

holli commented Nov 30, 2011

Hi,

oh. Sorry. Damn. ActiveModel support was removed at some point. It could probably be easily implemented again.

Currently the method is only included in ActiveRecord
https://github.com/holli/auto_strip_attributes/blob/master/lib/auto_strip_attributes.rb#L77 =>
ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord

I cant remember why it wasn't included in ActiveModel also. It might be because the way of fetching current values by
value = record[attribute]

I'm not sure if ActiveModel supported the same way of using array accessors. So it might need another way for activemodels e.g.
value = record.is_a?(ActiveRecord) ? record[attribute] : record.send(attribute)

https://github.com/holli/auto_strip_attributes/blob/master/lib/auto_strip_attributes.rb#L13
and same for setter.

So yeah, no support for ActiveModels currently. If you fix it somehow and tests pass, make sure to do a Pull request :)

cheers, Olli

@fredngo
Copy link

fredngo commented Aug 9, 2012

+1 for this.

@tetherit
Copy link

tetherit commented Jul 5, 2015

Any update on this? - I would like to use this with Mongoid which uses ActiveModel

@holli
Copy link
Owner

holli commented Jul 6, 2015

@bensheldon
Copy link
Contributor

It's not automatic, but it seems like it is possible to get AutoStripAttributes to work with ActiveModel as-is. As long as you:

  1. Manually extend your ActiveModel class with AutoStripAttributes
  2. Define bracket accessors

It must work because that's what the test suite does in order to mock Active Record: https://github.com/holli/auto_strip_attributes/blob/master/test/auto_strip_attributes_test.rb#L19-L32

...though it may not be widely recommended. I'd like to contribute some help to this issue. Would it be better for me to submit a PR to add the process to the docs, or for me to work on the test suite to move #8 forward so that we can automatically extend ActiveModel classes?

@holli
Copy link
Owner

holli commented Feb 15, 2016

True, good note.

Why would you think it might not be widely recommended? (I havent thought about it in a while so...)

Tests in that branch/pr would be nice. It just needs another test case that uses activemodel instead of activerecord so that all cases of https://github.com/holli/auto_strip_attributes/pull/8/files#diff-e020ef2416f83368a5ae745bf7867082R12 are tested.

.. and ofcours docs updated :)

@jacek213
Copy link

+1 for activemodel usage...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants