Skip to content

Commit

Permalink
Use original value for x-editable while showing formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
mlt committed Jun 8, 2020
1 parent 8778c2c commit 125aa13
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions lib/tabulatr/renderer/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,39 @@ def checkbox?() false end
def action?() false end

def value_for(record, view)
val = value_for_imp(record, view)
val = principal_value(record, view)
formatted = if self.col_options.format.present?
if val.respond_to?(:to_ary)
val.map do |v|
format_value(v, view)
end
else
format_value(val, view)
end
else
val
end
options = col_options.editable.is_a?(Hash) ? col_options.editable.with_indifferent_access : {}
# .to_s ? somehow 'false' renders to nothing
return "#{val}" unless col_options.editable and proxy.editable?(view, name.to_s)
return "#{formatted}" unless col_options.editable and proxy.editable?(view, name.to_s)
want_select = options.has_key?(:type) and options[:type].to_sym.eql?(:select)
# Typically we edit underlying association object unless we are selecting an item
if association?
assoc = table_name.to_s.split('-').map(&:to_sym)
a = assoc.pop if want_select
record = assoc.reduce(record) { |cur,nxt| cur.try(:send, nxt) }
if want_select
value = record.try(a).try(:id)
val = record.try(a).try(:id)
source = options.delete(:source)
if source
source = view.instance_eval(source.to_s) if source.is_a?(Symbol)
else
klass = record.class.reflect_on_association(a).klass
source = klass ? view.polymorphic_path(klass, format: :json) : default_source_for(val)
source = klass ? view.polymorphic_path(klass, format: :json) : default_source_for(formatted)
end
end
end
return "#{val}" if record.nil?
return "#{formatted}" if record.nil?
options.delete(:data)
nested = options.delete(:nested)
nid = options.delete(:nid)
Expand All @@ -104,16 +115,16 @@ def value_for(record, view)
else
url = view.send(url, record.id)
end
type = options.delete(:type){ default_type_for(val) }
view.content_tag :span, val, :class => :editable,
type = options.delete(:type){ default_type_for(formatted) }
view.content_tag :span, formatted, :class => :editable,
id: name,
:data => {
url: url,
name: name,
pk: record.id,
title: title,
placeholder: placeholder,
value: (value if want_select),
value: val,
source: (source if want_select),
nested: nested,
nid: nid,
Expand All @@ -122,21 +133,6 @@ def value_for(record, view)
}.merge(options.symbolize_keys).compact
end

def value_for_imp(record, view)
val = principal_value(record, view)
if self.col_options.format.present?
if val.respond_to?(:to_ary)
val.map do |v|
format_value(v, view)
end
else
format_value(val, view)
end
else
val
end
end

def principal_value(record, view)
if output
view.instance_exec(record, &output)
Expand Down

0 comments on commit 125aa13

Please sign in to comment.