Skip to content

Commit

Permalink
Merge pull request #72 from lucascaton/generator
Browse files Browse the repository at this point in the history
Fix/improve enum generator
  • Loading branch information
lucascaton authored Aug 21, 2017
2 parents 5feca81 + 31db76a commit fbad428
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/generators/enumerate_it/enum/enum_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Generators
class EnumGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

argument :attributes, type: 'array', default: []
argument :attributes, type: 'array'

class_option :singular, type: 'string', desc: 'Singular name for i18n'

Expand All @@ -14,12 +14,12 @@ def create_locale
template 'locale.yml', File.join('config/locales', "#{singular_name}.yml")
end

desc 'Creates an initializer file that extends ActiveRecord::Base with Enumerate_it'
desc 'Creates the enumeration'
def create_enumerate_it
template 'enumerate_it.rb', File.join('app/enumerations', "#{singular_name}.rb")
end

protected
private

def default_lang
options[:lang]
Expand Down
4 changes: 3 additions & 1 deletion lib/generators/enumerate_it/enum/templates/enumerate_it.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class <%= class_name %> < EnumerateIt::Base
<% if fields.first.is_a?(Array) %>associate_values <%= fields.map {|field, value| ":#{field} => #{value}"}.join(', ') %><% else %>associate_values <%= fields.map {|field| ":#{field}"}.join(', ') %><% end %>
associate_values(
<%= fields.map { |field, value| value ? "#{field}: #{value}" : ":#{field}"}.join(",\n ") %>
)
end

0 comments on commit fbad428

Please sign in to comment.