class TaskForm < Netzke::Basepack::Form
def configure(c)
super
c.model = "Task"
c.items = [
:name,
:priority,
:notes,
:due,
:done,
:first_name,
:last_name
]
c
end
require 'netzke/basepack/data_adapters/active_record_adapter'
module Netzke::Basepack::DataAdapters
class ActiveRecordAdapter < AbstractAdapter
def set_record_value_for_attribute(r, a, v, role = :default)
v = v.to_time_in_current_zone if v.is_a?(Date) # convert Date to Time
if a[:setter]
a[:setter].call(r, v)
elsif r.respond_to?("#{a[:name]}=")
# && attribute_mass_assignable?(a[:name], role) r.send("#{a[:name]}=", v)
elsif association_attr?(a)
split = a[:name].to_s.split(/\.|__/)
if a[:nested_attribute]
# We want:
# set_value_for_attribute({:name => :assoc_1__assoc_2__method, :nested_attribute => true}, 100)
# =>
# r.assoc_1.assoc_2.method = 100
split.inject(r) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
else
if split.size == 2
# search for association and assign it to r
assoc = @model_class.reflect_on_association(split.first.to_sym)
assoc_method = split.last
if assoc
if assoc.macro == :has_one
assoc_instance = r.send(
assoc.name)
if assoc_instance
assoc_instance.send("#{assoc_method}=", v)
assoc_instance.save # what should we do when this fails?..
else
# what should we do in this case?
end
else
# set the foreign key to the passed value
# not that if a negative value is passed, we reset the association (set it to nil)
r.send("#{assoc.foreign_key}=", v.to_i < 0 ? nil : v) if attribute_mass_assignable?(assoc.foreign_key, role)
end
else
logger.debug "Netzke::Basepack: Association #{assoc} is not known for class #{@data_class}"
end
else
logger.debug "Netzke::Basepack: Wrong attribute name: #{a[:name]}"
end
end
end
end
end
end
end