hashrocket / decent_exposure

A helper for creating declarative interfaces in controllers
MIT License
1.81k stars 107 forks source link

:ancestor option causes wrong scope on singular expose #114

Closed smostovoy closed 9 years ago

smostovoy commented 9 years ago

Hey, I'm trying to scope records. It's a one-to-many ralation like Company<-Employee.

expose(:company) { current_company }
expose(:employees, ancestor: :company) 
expose(:employee, ancestor: :company)  

In this example the last one item - singular - doesn't work as it tries to find/build record on ancestor instant object:

  NoMethodError - undefined method `new' for #<Company:0x007fae8a405278>

Seems like the problem is in https://github.com/hashrocket/decent_exposure/blob/master/lib/decent_exposure/active_record_strategy.rb#L25

 def ancestor_scope
      if plural?
        controller.send(options[:ancestor]).send(inflector.plural)
      else
        controller.send(options[:ancestor])
      end
    end

If it's not plural then we get only ancestor scope and find/build record is failed in https://github.com/hashrocket/decent_exposure/blob/master/lib/decent_exposure/active_record_strategy.rb#L58

    def singular_resource
      if id
        scope.send(finder, id)
      else
        scope.new
      end
    end

I can make a pull request if you agree that it should be fixed.

sjchmiela commented 9 years ago

Try removing the ancestor: :company from singular employee.