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

array :things; model; end no longer infers class name Thing #224

Closed
aaronjensen opened this issue Sep 3, 2014 · 3 comments
Closed

array :things; model; end no longer infers class name Thing #224

aaronjensen opened this issue Sep 3, 2014 · 3 comments
Assignees
Labels

Comments

@aaronjensen
Copy link

class Thing; end

class Example < ActiveInteraction::Base
  array :things do
    model # class: Thing
  end

  def execute
    inputs
  end
end

Example.run(things: [Thing.new])
# ActiveInteraction::InvalidClassError: "Things"
@AaronLasseigne
Copy link
Owner

Should it work this way? Should the name given to the array assume it's a model type by default?

@tfausak
Copy link
Collaborator

tfausak commented Sep 23, 2014

I think it should work this way. The name of the filter inside an array doesn't have any meaning. It could be called self or whatever. If the filter happens to be a model filter, it will do the right thing by being singularized. If it's not a model, it won't really matter.

class A < ActiveInteraction::Base
  array :ids do
    integer
  end
end

A.filters[:ids].filters
# Current:
#   => {:ids=>#<ActiveInteraction::IntegerFilter:0x... @name=:ids, ...>}
# Proposed:
#   => {:id=>#<ActiveInteraction::IntegerFilter:0x... @name=:id, ...>}

class B < ActiveInteraction::Base
  array :objects do
    model
  end
end

B.filters[:objects].filters
# Current:
#   => {:objects=>#<ActiveInteraction::ModelFilter:0x... @name=:objects, ...>}
# Proposed:
#   => {:object=>#<ActiveInteraction::ModelFilter:0x... @name=:object, ...>}

And for what it's worth, it's a pretty simple change.

diff --git a/lib/active_interaction/filters/array_filter.rb b/lib/active_interaction/filters/array_filter.rb
index 98ed4e1..2b39f3f 100644
--- a/lib/active_interaction/filters/array_filter.rb
+++ b/lib/active_interaction/filters/array_filter.rb
@@ -41,11 +41,11 @@ module ActiveInteraction

     def method_missing(*, &block)
       super do |klass, names, options|
-        filter = klass.new(name, options, &block)
+        filter = klass.new(name.to_s.singularize.to_sym, options, &block)

         validate(filter, names)

-        filters[name] = filter
+        filters[filter.name] = filter
       end
     end

@AaronLasseigne
Copy link
Owner

ok

@tfausak tfausak self-assigned this Sep 23, 2014
tfausak added a commit that referenced this issue Sep 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants