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
Rule::RULE_TYPES.inject({}){|r,s| r[s.to_s.humanize]=s;r}
steved
June 22, 2010, June 22, 2010 20:37, permalink
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
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.
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.