File: install_generator.rb

package info (click to toggle)
ruby-jquery-datatables-rails 3.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,288 kB
  • ctags: 391
  • sloc: ruby: 86; makefile: 2
file content (75 lines) | stat: -rw-r--r-- 2,784 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'rails/generators'

module Jquery
  module Datatables
    module Generators
      class InstallGenerator < ::Rails::Generators::Base
        desc "This generator installs jQuery dataTables to the Asset Pipeline"
        argument :style, :type => :string, :default => 'regular'

        def add_assets
          js_manifest = 'app/assets/javascripts/application.js'
          css_manifest = 'app/assets/stylesheets/application.css'
          js_strings = js_assets_map[style.to_sym].join

          insert_into_file js_manifest, js_strings, :after => "jquery_ujs\n" if File.exists?(js_manifest)
          insert_css_strings(css_manifest) if File.exists?(css_manifest)
        end

        private
        def insert_css_strings(css_manifest)
          content = File.read(css_manifest)
          css_strings = css_assets_map[style.to_sym].join

          if requires_tree(content)
            insert_into_file css_manifest, css_strings, :after => "require_tree .\n"
          elsif requires_self(content)
            insert_into_file css_manifest, css_strings, :before => " *= require_self\n"
          else
            insert_into_file css_manifest, css_strings, :before => " */"
          end
        end

        def requires_tree(content)
          content.match(/require_tree\s+\.\s*$/)
        end

        def requires_self(content)
          content.match(/require_self\s*$/)
        end

        def js_assets_map
          {
            :regular => ["//= require dataTables/jquery.dataTables\n"],
            :bootstrap2 => [
              "//= require dataTables/jquery.dataTables\n",
              "//= require dataTables/bootstrap/2/jquery.dataTables.bootstrap\n"
            ],
            :bootstrap3 => [
              "//= require dataTables/jquery.dataTables\n",
              "//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap\n"
            ],
            :foundation => [
              "//= require dataTables/jquery.dataTables\n",
              "//= require dataTables/jquery.dataTables.foundation\n"
            ],
            :responsive => [
              "//= require dataTables/jquery.dataTables\n",
              "//= require dataTables/extras/dataTables.responsive\n"
            ]
          }
        end

        def css_assets_map
          {
            :regular => [" *= require dataTables/jquery.dataTables\n"],
            :bootstrap2 => [" *= require dataTables/bootstrap/2/jquery.dataTables.bootstrap\n"],
            :bootstrap3 => [" *= require dataTables/bootstrap/3/jquery.dataTables.bootstrap\n"],
            :foundation => [" *= require dataTables/jquery.dataTables.foundation\n"],
            :responsive => [" *= require dataTables/extras/dataTables.responsive\n"]
          }
        end
      end
    end
  end
end