简体   繁体   中英

Rails: refactor code with blocks

I use Rails 4.2 and i want to refactor my helper method to get rid of duplicate code:

In app/helpers/admin/tasks_helper.rb

def chosen_select(name, method, chzes, selected = nil, options = {}, html_options = {})
  options[:value_method] ||= :id
  options[:text_method] ||= :name
  if options.key?(:placeholder)
    html_options['data-placeholder'.intern] = options[:placeholder]
    options.delete(:placeholder)
  end
  if html_options.key?(:class)
    html_options[:class] = 'chosen-select ' + html_options[:class]
  else
    html_options[:class] = 'chosen-select'
  end
  chzes = options_from_collection_for_select(chzes, options[:value_method], options[:text_method], selected)
  options.delete(:value_method)
  options.delete(:text_method)
  select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end

def chosen_select_array(name, method, chzes, selected = nil, options = {}, html_options = {})
  options[:value_method] ||= :id
  options[:text_method] ||= :name
  if options.key?(:placeholder)
    html_options['data-placeholder'.intern] = options[:placeholder]
    options.delete(:placeholder)
  end
  if html_options.key?(:class)
    html_options[:class] = 'chosen-select ' + html_options[:class]
  else
    html_options[:class] = 'chosen-select'
  end
  chzes = options_for_select(chzes, selected)
  options.delete(:value_method)
  options.delete(:text_method)
  select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end

I my view i have a lot of method calls like app/views/admin/tasks/index.html.erb

  <%= chosen_select(:select, :project_id, [TaskFilterOptgroups.active_projects, TaskFilterOptgroups.inactive_projects] , @task_filter_configuration.project_id, {:include_blank => true, :placeholder => 'Project'}, {'data-last-project_id' => @task_filter_configuration.project_id, :style => 'width: 150px;'}) %>

so that i don't want to change my method calls in the view. My attempt is to make a generic method "chosen_select_generic" that will be called from the specific method like "chosen_select":

def chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {})
  options[:value_method] ||= :id
  options[:text_method] ||= :name
  if options.key?(:placeholder)
    html_options['data-placeholder'.intern] = options[:placeholder]
    options.delete(:placeholder)
  end
  if html_options.key?(:class)
    html_options[:class] = 'chosen-select ' + html_options[:class]
  else
    html_options[:class] = 'chosen-select'
  end
  # 2 different chzes in 2 methods:
  # 1) chosen_select(...)
  # chzes = options_from_collection_for_select(chzes, options[:value_method], options[:text_method], selected)
  # 2) chosen_select_array(...)
  # chzes = options_for_select(chzes, selected)     
  yield chzes 
  options.delete(:value_method)
  options.delete(:text_method)
  select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end

and then chosen_select could look like:

def chosen_select(name, method, chzes, selected = nil, options = {}, html_options = {})
  chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {}) do |contents|
    chzes = option_groups_from_collection_for_select(chzes, :entries, :status, options[:value_method], options[:text_method], selected)
  end
end

But this doesn't work. How can i extract the duplicate code in a block without changing the method calls in the view?

This assignment in your block won't do what you think:

chzes = option_groups_from_collection_for_select(...)

It creates a new local variable instead of changing external one. If it's the only changeable piece here, then you can just return it from the block:

chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {}) do |chzes|
  option_groups_from_collection_for_select(chzes, :entries, :status, options[:value_method], options[:text_method], selected)
end

And receive the value in your generic method like this:

chzes = yield(chzes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM