PiRSquared17 / activescaffold

Automatically exported from code.google.com/p/activescaffold
MIT License
0 stars 0 forks source link

activescaffold and ruby 1.9.1 #678

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create a rails app
2. install activescaffold plugin
3. include an activescaffold config block to a controller

What is the expected output? What do you see instead?
expected activescaffolded model output, got an exception

What version (or revision) of the product are you using?
latest github checkout 4c348e90194064b5acaa21d2d60dda6015501d0d

If this bug causes an exception, please paste at least the first 20 lines
below.
Processing ApplicationController#index (for ::ffff:172.16.229.1 at
2009-04-03 15:41:32) [GET]

NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.columns=):
  app/controllers/admin/item_promotions_controller.rb:4:in `block in
<class:ItemPromotionsController>'
  app/controllers/admin/item_promotions_controller.rb:3:in
`<class:ItemPromotionsController>'
  app/controllers/admin/item_promotions_controller.rb:1:in `<top (required)>'
  <internal:prelude>:8:in `synchronize'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Rendered rescues/_trace (44.9ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)

Code extract : 
class Admin::ItemPromotionsController < ApplicationController
  layout 'admin'
  active_scaffold :item_promotions do |config|
    config.columns = [ :year, :week, :item]
    config.list.columns.exclude :year

    config.columns[:item].form_ui = :select
    config.list.sorting = [{:week => :desc}, {:item_id => :asc}]
  end
end

Original issue reported on code.google.com by dale.hof...@gmail.com on 3 Apr 2009 at 1:46

GoogleCodeExporter commented 9 years ago
Is this still an issue with 677 being fixed?

Original comment by mr.ga...@gmail.com on 6 Apr 2009 at 4:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This is still the case. Im using todays clone of active_scaffold, with Ruby 
1.9.1 
and Rails 2.3.2.

Example:
active_scaffold :table do |config|
# config == nil
  config.columns.exclude :created_at, :updated_at
# errror raised
end

test = active_scaffold :table
# test == [:create, :list, :search, :update, :delete, :show, :nested, :subform]

Original comment by Sickboy....@gmail.com on 13 Apr 2009 at 12:36

GoogleCodeExporter commented 9 years ago
Looks like it works if you do this (for example):

active_scaffold :table do
  self.columns.exclude :created_at, :updated_at
end

instead of:

active_scaffold :table do |config|
  config.columns.exclude :created_at, :updated_at
end

Original comment by McClella...@gmail.com on 21 Apr 2009 at 7:07

GoogleCodeExporter commented 9 years ago

Original comment by mr.ga...@gmail.com on 24 Apr 2009 at 3:02

GoogleCodeExporter commented 9 years ago
So does that mean what I said above is the new way to do things ... ? Shouldn't 
the
docs be updated to reflect that change? Isn't this kind of a biggie?

Original comment by McClella...@gmail.com on 24 Apr 2009 at 3:06

GoogleCodeExporter commented 9 years ago
It's a ruby change in instance_eval, we can't fix it, and I think self.colums 
works
in ruby 1.8 too. Please, add examples in the wiki docs where you think is 
needed.

Original comment by sergio.c...@gmail.com on 24 Apr 2009 at 3:33

GoogleCodeExporter commented 9 years ago
This is also an issue with ActiveScaffold.set_defaults, the suggested fix above 
(remove |config| and do 
'self.config') does not work for set_defaults.

This will have to be fully resolved.

I'm sure that the normal syntax _can_ work on Ruby 1.9, since all the Rails 
stuff (like Routes) still works. It might 
mean a change in the eval process for ActiveScaffold, but I'm sure it is 
possible.

Original comment by adam.q.salter@gmail.com on 8 Sep 2009 at 4:25

GoogleCodeExporter commented 9 years ago
The problem is block is eval in its own binding and in configuration binding, so
instance_eval is used. instance_eval was changed in ruby 1.9 and it doesn't 
work in
that way. Rails stuff like routes doesn't use instance_eval, they pass the 
object to
the block.

Probably the best way would be passing self to the block and removing
method_missing, so block will be eval only in its own binding, but it would 
break
backwards compatibility, for example I have some code which relies in this 
feature,
although I did it without be aware of it (I used model inside the block, 
instead of
config.model)

I have changed instance_eval to instance_exec passing self to the block. It 
works in
ruby 1.8 (due to active_support adds instance_exec which is a ruby 1.9 method). 
Can
you try with ruby 1.9?

Original comment by sergio.c...@gmail.com on 8 Sep 2009 at 10:21

GoogleCodeExporter commented 9 years ago
Yes that works on first blush (didn't run tests):

Change line 11 of lib/active_scaffold/configurable.rb to:
    ret = configuration_block.call(self)

Wow, I tried experimenting with this previously and couldn't quite work it 
out... yields and block and calls... :)

Original comment by adam.q.salter@gmail.com on 8 Sep 2009 at 10:34

GoogleCodeExporter commented 9 years ago
Umm.. looking at your comment I suspect I got it wrong... what was the exact 
code you wanted me to try?

Original comment by adam.q.salter@gmail.com on 8 Sep 2009 at 10:42

GoogleCodeExporter commented 9 years ago
I'm happy to run any tests you want against ruby 1.9, so if you point me to 
your repo once you apply change, I'll 
fork and test.

Original comment by adam.q.salter@gmail.com on 8 Sep 2009 at 10:46

GoogleCodeExporter commented 9 years ago
ret = configuration_block.call(self)
This line can work, but then you can't use model (while you can use with ruby 
1.8
now), you must use config.model, so that line breaks backwards compatibility.

I want you try
ret = instance_exec(self, &configuration_block)
which is in master

Original comment by sergio.c...@gmail.com on 8 Sep 2009 at 10:48

GoogleCodeExporter commented 9 years ago
Yes that works with method missing as well.

so both model and config.model work.

Thanks for quick response.

Original comment by adam.q.salter@gmail.com on 8 Sep 2009 at 11:18