PiRSquared17 / activescaffold

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

If config.cache_classes = true then column weight is disregarded when use exclude and add on column #732

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Implement an active_scaffold controller that defines column weights and
that excludes and then adds a column
2. With config.cache_classes = false in development.rb, after exclude
column in one request and add same column in second request, the columns
appear in the order by their weight, as expected
3. In development.rb set config.cache_classes = true
4. Restart Rails
5. With config.cache_classes = true in development.rb, after exclude column
in one request and add same column in second request, the readded column
appears at the end of the columns, which is not in order by weight

Attaching example project displaying issue. Run:

* rails db:migrate
* ./script/server
* Hit localhost:3000
* Create a record or two
* Hit localhost:3000/?foo=1
* Hit localhost:3000/?bar=1
* Notice how middle record moves to end.

What is the expected output? What do you see instead?

Expect that column weight should work if config.cache_classes = true.

What version (or revision) of the product are you using?

Current (2010-02-26):
script/plugin install git://github.com/activescaffold/active_scaffold.git

If this bug causes an exception, please paste at least the first 20 lines
below.
(none)

Original issue reported on code.google.com by garyswea...@gmail.com on 26 Feb 2010 at 9:44

Attachments:

GoogleCodeExporter commented 9 years ago
When you call exclude or add in config.action.columns, columns are copied from
config.columns, so weights set after that call aren't used.

Original comment by sergio.c...@gmail.com on 1 Mar 2010 at 8:56

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
(removed previous comment)

Sergio,

Thanks for your response!

So is the following correct?

* weights are useful when using config.cache_classes = false, because column 
exclude
and re-add retain the weight, so the columns stay in the right order.
* If using config.cache_classes = true, weights are only useful prior to a 
column
exclude and re-add. After a column re-add, if using config.cache_classes = 
true, you
must redefine config.columns after the re-add of the column?

Thanks for all of your work on active scaffold. It is extremely useful!

Gary

Original comment by garyswea...@gmail.com on 1 Mar 2010 at 2:28

GoogleCodeExporter commented 9 years ago
Sergio,

As a workaround for weights not working when config.cache_classes = false, I 
tried
specifying active_scaffold_config.columns = [ :my_first_column_name,
:my_second_column_name, :my_third_column_name, :my_fourth_column_name ] after 
the
column exclude and re-add, and it doesn't work.

Note that I'm trying to do active_scaffold_config.list.columns.exclude (symbol 
name
of column) and active_scaffold_config.list.columns.add (symbol name of column) 
in a
method called by the before_filter and then doing 
active_scaffold_config.columns = [
:my_first_column_name, :my_second_column_name, :my_third_column_name,
:my_fourth_column_name ].

Could you provide a workaround to set column order from method called by a 
before_filter?

Best Regards,

Gary

Original comment by garyswea...@gmail.com on 1 Mar 2010 at 8:36

GoogleCodeExporter commented 9 years ago
Issue 733 has been merged into this issue.

Original comment by sergio.c...@gmail.com on 2 Mar 2010 at 9:16

GoogleCodeExporter commented 9 years ago
The only difference with caching classes is changes are remembered for next 
request:
before_filter :add_column
def add_column
  active_scaffold_config.list.columns << :a_column if some_condition
end

That before_filter add a column when some_condition is true, but after adding 
it in
production environment (config.cache_classes enabled), it will be shown in next
requests, but in development mode, without caching classes, column only will be
shown when some_condition is true, because class is loaded in each request. 
That's
only to ease changing code in development without restarting the server, caching
classes should always be enabled in production.

You can try copying config.columns._inheritable at beginning in before filter, 
and
after excluding the columns. Instead of:

active_scaffold do |config|
  [set weights]
  config.list.columns.exclude :c1, :c2
end
def custom_before_filter
 if condition
  active_scaffold_config.list.columns.exclude :a
  active_scaffold_config.list.columns << :b
 else
  active_scaffold_config.list.columns.exclude :b
  active_scaffold_config.list.columns << :a
 end
end

use this:

def custom_before_filter
 active_scaffold_config.list.columns = active_scaffold_config.columns._inheritable
 active_scaffold_config.list.columns.exclude :c1, :c2
 if condition
  active_scaffold_config.list.columns.exclude :a
 else
  active_scaffold_config.list.columns.exclude :b
 end
end

Original comment by sergio.c...@gmail.com on 2 Mar 2010 at 9:23

GoogleCodeExporter commented 9 years ago
Sergio,

Thanks so much! That workaround worked for us with config.cache_classes enabled 
or
disabled.

I blogged your solution here as well as additional notes:
http://stufftohelpyouout.blogspot.com/2010/03/activescaffold-and-column-order.ht
ml

The only additional thing I could think of to say is that this workaround would 
seem
to only be a good option for those that don't need to accumulate column 
additions and
exclusions during the session, since it is resetting the columns to their 
original state.

Thanks again so much!

Gary

Original comment by garyswea...@gmail.com on 2 Mar 2010 at 3:39

GoogleCodeExporter commented 9 years ago
When you are changing columns per-request, in production you never know what is 
the
columns state, so you always have to exclude all columns you don't want, and add
columns you want. So the best way is resetting columns and exclude some of 
them, or
setting the columns array.

If we move to store changes in instance variables in order not to modify class
variables, you will always have the same state and it will be easier. Also AS 
would
be thread safe with that change probably, but it's hard and it's not my priority
ATM.

Original comment by sergio.c...@gmail.com on 3 Mar 2010 at 8:57

GoogleCodeExporter commented 9 years ago
Could you add that article to the wiki?

Original comment by sergio.c...@gmail.com on 3 Mar 2010 at 9:00

GoogleCodeExporter commented 9 years ago
Sergio,

I just added some info and the links to "API: Core" here (
http://wiki.github.com/activescaffold/active_scaffold/api-core ) under the 
"columns
local" section. Let me know if I should put it somewhere else or make additional
edits. Thanks again for your help!

Original comment by garyswea...@gmail.com on 3 Mar 2010 at 12:25