lib/spontaneous/field/base.rb in spontaneous-0.2.0.beta4 vs lib/spontaneous/field/base.rb in spontaneous-0.2.0.beta5
- old
+ new
@@ -119,10 +119,14 @@
def asynchronous?
false
end
def pending_value=(value)
+ set_pending_value(value, Spontaneous::Site.instance)
+ end
+
+ def set_pending_value(value, site)
values[:__pending__] = {
:value => value,
:version => version + 1,
:timestamp => timestamp
}
@@ -146,22 +150,22 @@
def clear_pending_value
values.delete(:__pending__)
end
- def process_pending_value
- if (pending = process_pending_value!)
+ def process_pending_value(site = Spontaneous::Site.instance)
+ if (pending = process_pending_value!(site))
cleanup_pending_value!(pending)
end
save
end
- def process_pending_value!
+ def process_pending_value!(site)
if has_pending_value?
pending = pending_value
@previous_values = values.dup
- set_value!(pending_value[:value])
+ set_value!(pending_value[:value], true, site)
pending
end
end
# Ensures that this update can still run
@@ -204,12 +208,12 @@
end
end
end
def reload
- Spontaneous::Content.scope! do
- Spontaneous::Field.find(self.id)
+ owner.model.scope! do
+ Spontaneous::Field.find(owner.model, id)
end
end
# Called by Field::Update before launching the background
# task that updates the field values.
@@ -222,24 +226,24 @@
def outputs
[:html, :plain]
end
- def process_value(value)
+ def process_value(value, site)
@modified = (@initial_value != value)
increment_version if @modified
- self.processed_values = generate_outputs(@unprocessed_value)
+ self.processed_values = generate_outputs(@unprocessed_value, site)
end
def set_value(v, process = true)
- set_value!(v, process)
+ set_value!(v, process, Spontaneous::Site.instance)
save
end
- def set_value!(v, process = true)
+ def set_value!(v, process = true, site = nil)
set_unprocessed_value(v)
- process_value(v) if process
+ process_value(v, site) if process
end
def modified!
owner.field_modified!(self) if owner
end
@@ -264,28 +268,28 @@
# value used to show conflicts between the current value and the value they're attempting to enter
def conflicted_value
unprocessed_value
end
- def generate_outputs(value)
+ def generate_outputs(value, site)
values = {}
- value = preprocess(value)
+ value = preprocess(value, site)
outputs.each do |output|
process_method = "generate_#{output}"
values[output] = \
if respond_to?(process_method)
- send(process_method, value)
+ send(process_method, value, site)
else
- generate(output, value)
+ generate(output, value, site)
end
end
values
end
# should be overwritten in subclasses that actually do something
# with the field value
- def preprocess(value)
+ def preprocess(value, site)
value
end
HTML_ESCAPE_TABLE = {
'&' => '&'
@@ -293,11 +297,11 @@
def escape_html(value)
value.to_s.gsub(%r{[#{HTML_ESCAPE_TABLE.keys.join}]}) { |s| HTML_ESCAPE_TABLE[s] }
end
- def generate(output, value)
+ def generate(output, value, site)
value
end
# attr_accessor :values
@@ -321,20 +325,20 @@
def to_s(format = :html)
value(format).to_s
end
- def render(format=:html, *args)
+ def render(format = :html, locals = {}, *args)
value(format)
end
- def render_using(renderer, format=:html, *args)
- render(format)
+ def render_using(renderer, format = :html, locals = {}, *args)
+ render(format, locals)
end
- def to_html(*args)
- render(:html, *args)
+ def to_html(locals = {})
+ value(:html)
end
def to_pdf(*args)
render(:pdf, *args)
end
@@ -458,9 +462,10 @@
def deserialize(params={}, default_values=true)
data = params.dup
unprocessed_value = data.delete(:unprocessed_value) || ""
processed_values = data.delete(:processed_values) || {}
+ set_unprocessed_value(unprocessed_value)
@processed_values = processed_values
set_value(unprocessed_value, default_values)
data.each do |property, value|
setter = "#{property}="
self.send(setter, value) if respond_to?(setter)