Module: Refinery::ExtensionGeneration

Included in:
EngineGenerator, FormGenerator
Defined in:
lib/refinery/extension_generation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/refinery/extension_generation.rb', line 4

def self.included(base)
  base.class_eval do
    argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"

    class_option :namespace, :type => :string, :default => nil, :banner => 'NAMESPACE', :required => false
    class_option :extension, :type => :string, :default => nil, :banner => 'ENGINE', :required => false
    class_option :i18n, :type => :array, :default => [], :required => false, :banner => "field field", :desc => 'Indicates generated fields'
    class_option :install, :type => :boolean, :default => false, :required => false, :banner => nil, :desc => 'Bundles and runs the generated generator, rake db:migrate, rake db:seed for you'

    remove_class_option :skip_namespace
  end
end

Instance Method Details

#attributes_for_translation_tableObject



66
67
68
# File 'lib/refinery/extension_generation.rb', line 66

def attributes_for_translation_table
  localized_attributes.inject([]) {|memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
end

#extension_class_nameObject



36
37
38
# File 'lib/refinery/extension_generation.rb', line 36

def extension_class_name
  @extension_class_name ||= extension_name.camelize
end

#extension_nameObject



32
33
34
# File 'lib/refinery/extension_generation.rb', line 32

def extension_name
  @extension_name ||= options[:extension].presence || singular_name
end

#extension_plural_class_nameObject



40
41
42
43
44
45
46
47
# File 'lib/refinery/extension_generation.rb', line 40

def extension_plural_class_name
  @extension_plural_class_name ||= if options[:extension].present?
    # Use exactly what the user requested, not a plural version.
    extension_class_name
  else
    extension_class_name.pluralize
  end
end

#extension_plural_nameObject



49
50
51
52
53
54
55
56
# File 'lib/refinery/extension_generation.rb', line 49

def extension_plural_name
  @extension_plural_name ||= if options[:extension].present?
    # Use exactly what the user requested, not a plural version.
    extension_name
  else
    extension_name.pluralize
  end
end

#image_attributesObject



74
75
76
# File 'lib/refinery/extension_generation.rb', line 74

def image_attributes
  @image_attributes ||= attributes.select { |a| a.type == :image }.uniq
end

#localized?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/refinery/extension_generation.rb', line 58

def localized?
  localized_attributes.any?
end

#localized_attributesObject



62
63
64
# File 'lib/refinery/extension_generation.rb', line 62

def localized_attributes
  @localized_attributes ||= attributes.select{|a| options[:i18n].include?(a.name)}
end

#names_for_attr_accessibleObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/refinery/extension_generation.rb', line 82

def names_for_attr_accessible
  @attributes_for_attr_accessible ||= attributes.map do |a|
    case a.type
    when :image, :resource
      "#{a.name}_id" unless a.name[-3..-1] == "_id"
    else
      a.name
    end
  end
end

#namespacingObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/refinery/extension_generation.rb', line 17

def namespacing
  @namespacing ||= if options[:namespace].present?
    # Use exactly what the user requested, not a pluralised version.
    options[:namespace].to_s.camelize
  else
    # If the user has passed an engine, we want to generate it inside of
    # that extension.
    if options[:extension].present?
      options[:extension].to_s.camelize
    else
      class_name.pluralize
    end
  end
end

#resource_attributesObject



78
79
80
# File 'lib/refinery/extension_generation.rb', line 78

def resource_attributes
  @resource_attributes ||= attributes.select { |a| a.type == :resource }.uniq
end

#string_attributesObject



70
71
72
# File 'lib/refinery/extension_generation.rb', line 70

def string_attributes
  @string_attributes ||= attributes.select {|a| /string|text/ === a.type.to_s}.uniq
end