Skip to content

Instantly share code, notes, and snippets.

@ehlyzov
Created April 23, 2009 04:43
Show Gist options
  • Save ehlyzov/100317 to your computer and use it in GitHub Desktop.
Save ehlyzov/100317 to your computer and use it in GitHub Desktop.
def calculate_params(context, method_name, params)
if params.kind_of?(Array)
context.send(method_name, *params)
elsif params.kind_of?(Hash)
context = context.send(method_name)
calculate_params(context, params.keys[0], params.values[0])
elsif params.respond_to?(:to_sym)
context.send(method_name, params)
else raise ArgumentError, "Unknown type of function params"
end
end
def serializable_record
returning(serializable_record = {}) do
serializable_names.each do |name|
if(params = methods_with_params[name])
serializable_record[name] = calculate_params(@record, name, params)
else
serializable_record[name] = @record.send(name)
end
end
add_includes do |association, records, opts|
# puts association.inspect
# puts records.inspect
# puts opts.inspect
# puts "============"
if records.is_a?(Enumerable)
serializable_record[association] = records.collect { |r| cl = self.class.new(r, opts); cl.serializable_record }
else
serializable_record[association] = self.class.new(records, opts).serializable_record
end
# puts serializable_record[association].inspect
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment