Class: ScaffoldAdminGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/scaffold_admin/scaffold_admin_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

FIXME: Should be proxied to ActiveRecord::Generators::Base Implement the required interface for Rails::Generators::Migration.



122
123
124
125
126
127
128
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 122

def self.next_migration_number(dirname) #:nodoc:
 if ActiveRecord::Base.timestamped_migrations
   Time.now.utc.strftime("%Y%m%d%H%M%S")
 else
   "%.3d" % (current_migration_number(dirname) + 1)
 end
end

Instance Method Details

#args_attributesObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 71

def args_attributes
  @model_attributes = []

  attributes.each do |arg|
    if arg.include?(':')
      @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
    end
  end

  @model_attributes.uniq!

end

#array_viewsObject



59
60
61
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 59

def array_views
  %w[edit _form index new show]
end

#class_nameObject



96
97
98
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 96

def class_name
  model_name.titlecase.gsub(/[\-\/" "]/, "")
end

#create_migrationObject



51
52
53
54
55
56
57
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 51

def create_migration
  if namespace_name
    migration_template "migration.rb", "db/migrate/create_#{namespace_underscore}_#{plural_name}.rb"
  else
    migration_template "migration.rb", "db/migrate/create_#{plural_name}.rb"
  end
end

#create_templatesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 25

def create_templates
  args_attributes

  if namespace_name
    template "model.rb", "app/models/#{namespace_underscore}/#{singular_name}.rb"
    template "controller.rb", "app/controllers/#{namespace_underscore}/#{plural_name}_controller.rb"
    template "helper.rb", "app/helpers/#{namespace_underscore}/#{plural_name}_helper.rb"

    array_views.each do |view|
      template "views/#{view}.html.erb", "app/views/#{namespace_underscore}/#{plural_name}/#{view}.html.erb"
    end

  else
    template "model.rb", "app/models/#{singular_name}.rb"
    template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
    template "helper.rb", "app/helpers/#{plural_name}_helper.rb"

    array_views.each do |view|
      template "views/#{view}.html.erb", "app/views/#{plural_name}/#{view}.html.erb"
    end

  end

  route "resources :#{plural_name}"
end

#model_attributes_namesObject

class_option :index, type: :boolean, default: true, desc: “Include index” puts index exit



15
16
17
18
19
20
21
22
23
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 15

def model_attributes_names
  @model_attributes_names = []

  attributes.each do |attribute|
    @model_attributes_names << attribute.gsub(/:(.*)/, "")
  end

  @model_attributes_names.join(', :')
end

#model_nameObject



84
85
86
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 84

def model_name
  split_scaffold_name[:model_name]
end

#module_nameObject



108
109
110
111
112
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 108

def module_name
  unless namespace_name.nil?
    namespace_name.titlecase.gsub(/[\-\/" "]/, "")
  end
end

#namespace_nameObject



88
89
90
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 88

def namespace_name
  split_scaffold_name[:namespace]
end

#namespace_underscoreObject



114
115
116
117
118
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 114

def namespace_underscore
  unless namespace_name.nil?
    namespace_name.underscore
  end
end

#plural_classObject



104
105
106
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 104

def plural_class
  class_name.pluralize
end

#plural_nameObject



100
101
102
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 100

def plural_name
  model_name.underscore.pluralize
end

#singular_nameObject



92
93
94
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 92

def singular_name
  model_name.underscore
end

#split_scaffold_nameObject



63
64
65
66
67
68
69
# File 'lib/generators/scaffold_admin/scaffold_admin_generator.rb', line 63

def split_scaffold_name
  path = {}
  namespace = scaffold_name.split('/')
  path[:model_name] = namespace.pop
  path[:namespace] = namespace.pop
  path
end