Skip to content

Commit

Permalink
Start of readonly column changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zakk4223 committed May 1, 2012
1 parent 091b997 commit 4efa37c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/active_scaffold/config/core.rb
Expand Up @@ -101,6 +101,10 @@ def label(options={})
# STI children models, use an array of model names
attr_accessor :sti_children


#Set an entire controller readonly
attr_accessor :readonly

##
## internal usage only below this point
## ------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions lib/active_scaffold/data_structures/column.rb
Expand Up @@ -73,6 +73,10 @@ def update_columns=(column_names)
cattr_accessor :send_form_on_update_column
attr_accessor :send_form_on_update_column

# mark the column as readonly, this disables it in the update/create forms
cattr_accessor :readonly
attr_accessor :readonly

# sorting on a column can be configured four ways:
# sort = true default, uses intelligent sorting sql default
# sort = false sometimes sorting doesn't make sense
Expand Down Expand Up @@ -342,6 +346,22 @@ def number_to_native(value)
end
end

def readonly=(roval)
@readonly = roval
if roval
#if this is an association and it is flagged as readonly
#disable the bits of the form that relate to adding new
#members
@allow_add_existing = false
@show_blank_record = false
end
end

def readonly
self.class.readonly || @readonly
end


protected

def initialize_sort
Expand Down
23 changes: 20 additions & 3 deletions lib/active_scaffold/helpers/form_column_helpers.rb
Expand Up @@ -5,13 +5,30 @@ module FormColumnHelpers
# This method decides which input to use for the given column.
# It does not do any rendering. It only decides which method is responsible for rendering.
def active_scaffold_input_for(column, scope = nil, options = {})
options = active_scaffold_input_options(column, scope, options)
options = update_columns_options(column, scope, options)
active_scaffold_render_input(column, options)
if column.readonly || @record.readonly?
active_scaffold_readonly_column_string(@record.send column.name)
else
options = active_scaffold_input_options(column, scope, options)
options = update_columns_options(column, scope, options)
active_scaffold_render_input(column, options)
end
end

alias form_column active_scaffold_input_for


def active_scaffold_readonly_column_string(col_val)
return nil unless col_val
if col_val.respond_to? :to_label
col_val.to_label
elsif col_val.is_a? Array
col_value.map {|v| active_scaffold_readonly_column_string(v)}.join(',')
else
col_val.to_s
end
end


def active_scaffold_render_input(column, options)
begin
# first, check if the dev has created an override for this specific field
Expand Down

0 comments on commit 4efa37c

Please sign in to comment.