Class: Datagrid::Scaffold

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::ResourceHelpers
Defined in:
lib/datagrid/scaffold.rb

Instance Method Summary collapse

Instance Method Details

#base_grid_fileObject


76
77
78
# File 'lib/datagrid/scaffold.rb', line 76

def base_grid_file
  "app/grids/base_grid.rb"
end

#create_scaffoldObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/datagrid/scaffold.rb', line 10

def create_scaffold
  unless file_exists?(base_grid_file)
    template "base.rb.erb", base_grid_file
  end
  template "grid.rb.erb", "app/grids/#{grid_class_name.underscore}.rb"
  if file_exists?(grid_controller_file)
    inject_into_file grid_controller_file, index_action, :after => %r{class .*#{grid_controller_class_name}.*\n}
  else
    template "controller.rb.erb", grid_controller_file
  end
  template "index.html.erb", view_file
  route(generate_routing_namespace("resources :#{grid_controller_short_name}"))
  unless defined?(::Kaminari) || defined?(::WillPaginate)
    gem 'kaminari'
  end
  in_root do
    {
      "css" => " *= require datagrid",
      "css.sass" => " *= require datagrid",
      "css.scss" => " *= require datagrid",
    }.each do |extension, string|
      file = "app/assets/stylesheets/application.#{extension}"
      if file_exists?(file)
        inject_into_file file, string + "\n", {:before => %r{.*require_self}} # before all
      end
    end
  end
end

#grid_class_nameObject


43
44
45
# File 'lib/datagrid/scaffold.rb', line 43

def grid_class_name
  file_name.camelize.pluralize + "Grid"
end

#grid_controller_class_nameObject


47
48
49
# File 'lib/datagrid/scaffold.rb', line 47

def grid_controller_class_name
  controller_class_name.camelize + "Controller"
end

#grid_controller_fileObject


51
52
53
# File 'lib/datagrid/scaffold.rb', line 51

def grid_controller_file
  Rails.root.join("app/controllers").join("#{grid_controller_class_name.underscore}.rb")
end

#grid_controller_short_nameObject


55
56
57
# File 'lib/datagrid/scaffold.rb', line 55

def grid_controller_short_name
  controller_file_name
end

#grid_model_nameObject


59
60
61
# File 'lib/datagrid/scaffold.rb', line 59

def grid_model_name
  file_name.camelize.singularize
end

#grid_param_nameObject


63
64
65
# File 'lib/datagrid/scaffold.rb', line 63

def grid_param_name
  grid_class_name.underscore
end

#grid_route_nameObject


80
81
82
# File 'lib/datagrid/scaffold.rb', line 80

def grid_route_name
  controller_class_name.underscore.gsub("/", "_") + "_path"
end

#index_actionObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/datagrid/scaffold.rb', line 84

def index_action
  indent(<<-RUBY)
def index
@grid = #{grid_class_name}.new(grid_params) do |scope|
  scope.page(params[:page])
end
end

protected

def grid_params
params.fetch(:#{grid_param_name}, {}).permit!
end
RUBY
end

#pagination_helper_codeObject


67
68
69
70
71
72
73
74
# File 'lib/datagrid/scaffold.rb', line 67

def pagination_helper_code
  if defined?(::WillPaginate)
    "will_paginate(@grid.assets)"
  else
    # Kaminari is default
    "paginate(@grid.assets)"
  end
end

#view_fileObject


39
40
41
# File 'lib/datagrid/scaffold.rb', line 39

def view_file
  Rails.root.join("app/views").join(controller_file_path).join("index.html.erb")
end