From ef25bb502a6d0354b071e16d583e861db9c6acc5 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Mon, 30 Jul 2012 23:58:56 +0400 Subject: [PATCH] [issue #565] refactoring, proposed by @alexander-machehin --- app/models/feedback.rb | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/models/feedback.rb b/app/models/feedback.rb index ba60e6dc4..af9f205a4 100644 --- a/app/models/feedback.rb +++ b/app/models/feedback.rb @@ -58,34 +58,31 @@ class Feedback end def to_s - str = "#<#{self.class} " - str += %w{ name email subject message }.inject('') do |s, e| - s << "#{e}: #{ send(e).inspect }, "; s - end - str[-2] = '>' - str[0..-1] + str = %w{ name email subject message }.map do |e| + "#{e}: #{ send(e).inspect }" + end.join(', ') + return "#<#{self.class} #{str}>" end class << self def create(attributes = nil, options = {}, &block) - if attributes.is_a?(Array) - attributes.collect { |attr| create!(attr, options, &block) } - else - object = new(attributes, options) - yield(object) if block_given? - object.perform_send - object - end + do_create(attributes, options, false, &block) end def create!(attributes = nil, options = {}, &block) + do_create(attributes, options, true, &block) + end + + protected + + def do_create(attributes = nil, options = {}, bang = false, &block) if attributes.is_a?(Array) - attributes.collect { |attr| create!(attr, options, &block) } + attributes.collect { |attr| do_create(attr, options, bang, &block) } else object = new(attributes, options) yield(object) if block_given? - object.perform_send! + bang ? object.perform_send! : object.perform_send object end end