31e2b397e15442a2d3ba42db7bd4584e

There has got to be a much cleaner way of writing this.

class Rule < ActiveRecord::Base
  RULE_TYPES = [:allowed_senders, :blocked_senders, :blocked_character_set, :custom]  
end

module RulesHelper

  def rule_type_options
    options = {}
    Rule::RULE_TYPES.each do |rule_type|
      options[rule_type.to_s.humanize] = rule_type
    end
    options
  end

end

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

pepepe

June 22, 2010, June 22, 2010 01:19, permalink

1 rating. Login to rate!
Rule::RULE_TYPES.inject({}){|r,s| r[s.to_s.humanize]=s;r}
D41d8cd98f00b204e9800998ecf8427e

steved

June 22, 2010, June 22, 2010 20:37, permalink

1 rating. Login to rate!
def rule_type_options
  keys_and_values = Rule::RULE_TYPES.map { |rt| [rt.to_s.humanize, rt] }.flatten
  Hash[*keys_and_values]
end
7855792dbc5f3b4c365344314e2b1ad6

arvanasse

June 30, 2010, June 30, 2010 13:18, permalink

No rating. Login to rate!

#inject is the best way to transform (reduce) an enumerable

class Rule < ActiveRecord::Base
  RULE_TYPES = [:allowed_senders, :blocked_senders, :blocked_character_set, :custom]  
end

module RulesHelper

  def rule_type_options
    Rule::RULE_TYPES.inject( {} ){|options, rule_type| collector.merge rule_type.to_s.humanize => rule_type }
  end

end
7855792dbc5f3b4c365344314e2b1ad6

arvanasse

June 30, 2010, June 30, 2010 13:19, permalink

No rating. Login to rate!

#inject is the best way to transform (reduce) an enumerable

... sorry, pepepe, I didn't read your solution first.

class Rule < ActiveRecord::Base
  RULE_TYPES = [:allowed_senders, :blocked_senders, :blocked_character_set, :custom]  
end

module RulesHelper

  def rule_type_options
    Rule::RULE_TYPES.inject( {} ){|options, rule_type| options.merge rule_type.to_s.humanize => rule_type }
  end

end

Your refactoring





Format Copy from initial code

or Cancel