Class: Kowl::AdminGenerator

Inherits:
Generators::Base show all
Defined in:
lib/kowl/generators/admin_generator.rb

Instance Method Summary collapse

Methods inherited from Generators::Base

default_source_root, source_paths

Methods included from Docker

#alpine_docker_dependencies, #app_js_volumes, #app_volumes, #db_volumes, #debian_database_dependencies, #debian_docker_dependencies, #docker_app_command, #docker_compose_database_string, #docker_databases, #docker_depends_on, #docker_port_watcher, #docker_redis_service, #docker_sidekiq_service, #docker_variables, #docker_volumes, #docker_webpacker_service, #dockerfile_database_args, #dockerfile_migration_snip, #js_volumes, #mysql_volumes, #postgresql_volumes, #redis_volumes

Methods included from Actions

#add_extension_routes, #add_package, #append_to_file, #database_route, #dev_config, #dup_file, #file_exists?, #mailer_gems, #mailer_route, #mk_dir, #move_file, #pry_gems, #rails_cmd, #remove_dir, #remove_file, #remove_gem, #replace_string_in_file, #robocop_test_engine, #sidekiq_route, #template_linter_gems

Instance Method Details

#add_trix_scssObject

Unless javascript is skipped this adds the TRIX SCSS the to administrate stylesheets



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kowl/generators/admin_generator.rb', line 69

def add_trix_scss
  return nil if options[:skip_javascript]

  trix_import_str = <<~TRIX_SCSS
    // Used for creating ActionText content editors
    @import 'trix/dist/trix';
    // Used for date/time selection boxes in the admin interface
    @import 'flatpickr/dist/flatpickr.min';

    // We need to override trix.css’s image gallery styles to accommodate the
    // <action-text-attachment> element we wrap around attachments. Otherwise,
    // images in galleries will be squished by the max-width: 33%; rule.
    .trix-content {
      .attachment-gallery {
        >action-text-attachment,
        >.attachment {
          flex: 1 0 33%;
          padding: 0 0.5em;
          max-width: 33%;
        }
        &.attachment-gallery--2,
        &.attachment-gallery--4 {
          >action-text-attachment,
          >.attachment {
            flex-basis: 50%;
            max-width: 50%;
          }
        }
      }
      action-text-attachment {
        .attachment {
          padding: 0 !important;
          max-width: 100% !important;
        }
      }
    }
    .field-unit--rich-text-area-field {
      .field-unit__field {
        width: 80%;
      }
    }
  TRIX_SCSS
  append_to_file('app/assets/stylesheets/administrate/application.scss', trix_import_str)
end

#copy_admin_viewsObject

Copy views for administrate dependant on if the app will use javascript or not



59
60
61
62
63
64
65
66
# File 'lib/kowl/generators/admin_generator.rb', line 59

def copy_admin_views
  if options[:skip_javascript]
    remove_file('app/views/admin/application/_javascript.html.erb')
  else
    copy_file('views/admin/views/application/_javascript.html.erb', 'app/views/admin/application/_javascript.html.erb')
  end
  template('views/layouts/admin.html.erb.tt', 'app/views/layouts/admin/application.html.erb')
end

#copy_dashboardsObject

We copy dashboards over after the fields are generates. Otherwise there will be an error about field types unknown (GravatarField)



37
38
39
40
41
42
43
44
# File 'lib/kowl/generators/admin_generator.rb', line 37

def copy_dashboards
  # Replace with dashboards showing less user data (this is because even admin and staff should be able to add, edit, or modify certain attributes)
  remove_dir('app/dashboards/')
  # This is because if UUID's are used the dashboards try to render them as integers.
  # Otherwise they'll be displayed as strings
  mk_dir('app/dashboards')
  %i[login_activity user rich_text_body].map { |dashboard| template "dashboards/#{dashboard}_dashboard.rb.tt", "app/dashboards/#{dashboard}_dashboard.rb" }
end

#generate_action_text_fieldsObject

Generate support for the action_text field type to be supported with administrate



30
31
32
33
# File 'lib/kowl/generators/admin_generator.rb', line 30

def generate_action_text_fields
  generate('administrate:field rich_text_area')
  copy_file('app/fields/rich_text_area_field.rb', force: true)
end

#generate_admin_dashboardObject

NOTE: In most of these I user the administrates native generator, before replacing them that way if any tests are generates they will be available as well This is skipped when using UUID’s because by default the dashbooards try to us integers for ID’s



18
19
20
# File 'lib/kowl/generators/admin_generator.rb', line 18

def generate_admin_dashboard
  generate('administrate:install') unless options[:uuid]
end

#generate_avatar_fieldsObject

Generate custom field attribute types to show a persons gravatar



23
24
25
26
27
# File 'lib/kowl/generators/admin_generator.rb', line 23

def generate_avatar_fields
  generate('administrate:field gravatar')
  remove_dir('app/fields')
  copy_file('app/fields/gravatar_field.rb', force: true)
end

#replace_admin_controllersObject

This is because admin controllers should only be viewable if a user is a superuser or staff member



53
54
55
56
# File 'lib/kowl/generators/admin_generator.rb', line 53

def replace_admin_controllers
  remove_dir('app/controllers/admin')
  directory('controllers/admin', 'app/controllers/admin', force: true)
end

#replace_field_viewsObject

this is to replace default generated gravatar views with ones that will actually show the user gravatars



47
48
49
50
# File 'lib/kowl/generators/admin_generator.rb', line 47

def replace_field_views
  remove_dir('app/views/fields')
  directory('views/fields', 'app/views/fields', force: true)
end