1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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 !
pepepe
June 22, 2010, June 22, 2010 01:19, permalink
1
Rule::RULE_TYPES.inject({}){|r,s| r[s.to_s.humanize]=s;r}
steved
June 22, 2010, June 22, 2010 20:37, permalink
1 2 3 4
def rule_type_options keys_and_values = Rule::RULE_TYPES.map { |rt| [rt.to_s.humanize, rt] }.flatten Hash[*keys_and_values] end
arvanasse
June 30, 2010, June 30, 2010 13:18, permalink
#inject is the best way to transform (reduce) an enumerable
1 2 3 4 5 6 7 8 9 10 11
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
arvanasse
June 30, 2010, June 30, 2010 13:19, permalink
#inject is the best way to transform (reduce) an enumerable
... sorry, pepepe, I didn't read your solution first.
1 2 3 4 5 6 7 8 9 10 11
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
There has got to be a much cleaner way of writing this.