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

#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
# File 'lib/datagrid/scaffold.rb', line 10

def create_scaffold
  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?(Rails.root.join(file))
        inject_into_file file, string + "\n", {:before => %r{.*require_self}} # before all
      end
    end
  end
end

#grid_class_nameObject



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

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

#grid_controller_class_nameObject



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

def grid_controller_class_name
  controller_class_name.camelize + "Controller"
end

#grid_controller_fileObject



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

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

#grid_controller_short_nameObject



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

def grid_controller_short_name
  controller_file_name
end

#grid_model_nameObject



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

def grid_model_name
  file_name.camelize.singularize
end

#grid_param_nameObject



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

def grid_param_name
  grid_class_name.underscore
end

#grid_route_nameObject



74
75
76
# File 'lib/datagrid/scaffold.rb', line 74

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

#index_actionObject



78
79
80
81
82
83
84
85
86
# File 'lib/datagrid/scaffold.rb', line 78

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

#pagination_helper_codeObject



64
65
66
67
68
69
70
71
72
# File 'lib/datagrid/scaffold.rb', line 64

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

end

#view_fileObject



36
37
38
# File 'lib/datagrid/scaffold.rb', line 36

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