Class: Templaty::CrudGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/templaty/crud_generator.rb

Instance Method Summary collapse

Instance Method Details

#controllerObject



28
29
30
31
32
33
# File 'lib/generators/templaty/crud_generator.rb', line 28

def controller
  from = 'crud/app/controllers/system/table_controller.rb.erb'
  to   = "app/controllers/system/#{options[:table]}_controller.rb"

  template(from, to)
end

#cssObject



35
36
37
38
39
40
41
42
# File 'lib/generators/templaty/crud_generator.rb', line 35

def css
  %w[form index].each do |name|
    from = "crud/app/assets/stylesheets/namespace/views/table/#{name}.scss.erb"
    to   = "app/assets/stylesheets/#{options[:namespace]}/views/#{options[:table]}/#{name}.scss"

    template(from, to)
  end
end

#hbsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/templaty/crud_generator.rb', line 44

def hbs
  %w[table.gridy.hbs.erb].each do |name|
    from = "crud/app/assets/templates/namespace/#{name}"
    to   = "app/javascript/templates/#{options[:namespace]}/#{options[:table]}.gridy.hbs"

    template(from, to)
  end

  `gulp hbs`
end

#i18nObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generators/templaty/crud_generator.rb', line 55

def i18n
  actions = %w[create destroy edit index show update]

  %w[en pt-BR ru].each do |locale|
    actions.each do |name|
      next if name == 'show' && options[:show_route] != true

      from = "crud/config/locales/#{locale}/namespace/table/#{name}.yml.erb"
      to   = "config/locales/#{locale}/#{options[:namespace]}/#{options[:table]}/#{name}.yml"

      template(from, to)
    end

    from = "crud/config/locales/#{locale}/model.yml.erb"
    to   = "config/locales/#{locale}/#{options[:table].singularize}.yml"

    template(from, to)
  end
end

#jsObject



75
76
77
78
79
80
81
82
# File 'lib/generators/templaty/crud_generator.rb', line 75

def js
  %w[form index].each do |name|
    from = "crud/app/javascripts/#{name}.js.erb"
    to   = "app/javascript/pages/#{options[:namespace]}/views/#{options[:table]}/#{name}.js"

    template(from, to)
  end
end

#modelObject



84
85
86
87
88
89
# File 'lib/generators/templaty/crud_generator.rb', line 84

def model
  from = 'crud/app/models/model.rb.erb'
  to   = "app/models/#{options[:table].singularize}.rb"

  template(from, to)
end

#pagesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/generators/templaty/crud_generator.rb', line 91

def pages
  %w[
    _form.html.erb
    _submenu.html.erb
    _title.html.erb
    edit.html.erb
    gridy.json.jbuilder
    index.html.erb
    new.html.erb
  ].each do |name|
    from = "crud/app/views/namespace/table/#{name}.erb"
    to = "app/views/#{options[:namespace]}/#{options[:table]}/#{name}"

    template(from, to)
  end
end

#route_gridyableObject



120
121
122
123
124
125
126
# File 'lib/generators/templaty/crud_generator.rb', line 120

def route_gridyable
  route %(
    concern :gridyable do
      get :gridy, defaults: { format: :json }, on: :collection
    end
  )
end

#route_resourceObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/templaty/crud_generator.rb', line 108

def route_resource
  content = <<~HEREDOC
    resources :#{options[:table]},
      concerns:   :gridyable,
      #{options[:show_route] ? nil : 'except:     :show,'}
      path:       :#{options[:path]},
      path_names: { new: :novo, edit: :editar }
  HEREDOC

  route content, namespace: options[:namespace]
end

#seedObject



128
129
130
131
132
133
# File 'lib/generators/templaty/crud_generator.rb', line 128

def seed
  from = 'crud/db/seeds/pd/model.rb.erb'
  to   = "db/seeds/pd/#{options[:table].singularize}.rb"

  template(from, to)
end

#sharedObject



135
136
137
138
139
140
141
142
# File 'lib/generators/templaty/crud_generator.rb', line 135

def shared
  %w[_model.html.erb].each do |name|
    from = "crud/app/views/shared/icons/#{name}.erb"
    to   = "app/views/shared/guide/_#{options[:table].singularize}.html.erb"

    template(from, to)
  end
end

#specObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/generators/templaty/crud_generator.rb', line 144

def spec
  %w[
    spec/controllers/namespace/model/create_spec.rb
    spec/controllers/namespace/model/destroy_spec.rb
    spec/controllers/namespace/model/edit_spec.rb
    spec/controllers/namespace/model/gridy/json_spec.rb
    spec/controllers/namespace/model/gridy/unlogged_spec.rb
    spec/controllers/namespace/model/index_spec.rb
    spec/controllers/namespace/model/new_spec.rb
    spec/controllers/namespace/model/update_spec.rb
    spec/features/namespace/model/create_spec.rb
    spec/features/namespace/model/destroy_spec.rb
    spec/features/namespace/model/gridy/initial_spec.rb
    spec/features/namespace/model/gridy/search_spec.rb
    spec/features/namespace/model/gridy/sort_spec.rb
    spec/features/namespace/model/update_spec.rb
    spec/models/model/list_spec.rb
  ].each do |path|
    from = "crud/#{path}.erb"
    to   = path.sub('namespace', options[:namespace]).sub('model', options[:table].singularize)

    template(from, to)
  end
end

#warningObject



169
170
171
172
173
174
175
176
177
# File 'lib/generators/templaty/crud_generator.rb', line 169

def warning
  puts("\n=== Plese edit the following files: ===\n\n") # rubocop:disable Rails/Output

  %w[en pt-BR ru].each do |locale|
    puts("code config/locales/#{locale}/#{options[:table].singularize}.yml:5") # rubocop:disable Rails/Output
  end

  puts("code db/seeds/pd/#{options[:table].singularize}.rb") # rubocop:disable Rails/Output
end