Class: Qbrick::CustomModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/qbrick/custom_model_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_controllerObject



61
62
63
64
# File 'lib/generators/qbrick/custom_model_generator.rb', line 61

def add_controller
  generate 'controller', controller_name, '--no-helper --no-assets --no-test-framework'
  gsub_file "app/controllers/#{controller_name}_controller.rb", 'ApplicationController', 'Qbrick::BaseController'
end

#add_model_and_migrationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/qbrick/custom_model_generator.rb', line 33

def add_model_and_migration
  generate 'model', model_name, ARGV[1..-1].join(' '), '--no-test-framework'
  inject_into_file "app/models/#{model_name}.rb", before: 'class' do
    "require 'qbrick/cms_model'\n\n"
  end

  inject_into_file "app/models/#{model_name}.rb", before: 'end' do
    <<-eos.gsub(/^ {8}/, '').chomp
      include Qbrick::CMSModel

      # TODO: Define what attributes are shown in the form and permitted by strong parameters
      editable_attributes #{attribute_keys_as_string}

      # TODO: Define what attributes are shown in the index view
      index_attributes #{attribute_keys_as_string}

     eos
  end
end


25
26
27
28
29
30
31
# File 'lib/generators/qbrick/custom_model_generator.rb', line 25

def add_navigation_link
  nav_file = 'app/views/qbrick/cms/backend/_main_navigation.html.haml'

  inject_into_file nav_file, after: '%li= link_to Qbrick::Page.model_name.human(:count => 2), qbrick.cms_pages_path' do
    "\n  %li= link_to t('cms.navigation_title.#{model_name}'), #{route_name}_path"
  end
end

#add_resource_translationsObject



53
54
55
56
57
58
59
# File 'lib/generators/qbrick/custom_model_generator.rb', line 53

def add_resource_translations
  I18n.available_locales.each do |locale|
    I18n.with_locale(locale) do
      template 'translations/resource.yml.erb', "config/locales/#{locale}/#{model_name}.yml"
    end
  end
end

#add_routeObject



21
22
23
# File 'lib/generators/qbrick/custom_model_generator.rb', line 21

def add_route
  generate 'resource_route', resource_route_name
end

#set_up_custom_models_baseObject



12
13
14
15
16
17
18
19
# File 'lib/generators/qbrick/custom_model_generator.rb', line 12

def set_up_custom_models_base
  return if custom_models_base_already_installed?

  setup_base_controller
  setup_base_views
  setup_navigation
  setup_translation_file
end

#source_pathsObject



5
6
7
8
9
10
# File 'lib/generators/qbrick/custom_model_generator.rb', line 5

def source_paths
  [
    File.join(File.dirname(__FILE__), '../../../app/views/qbrick/cms/backend/'),
    File.join(File.dirname(__FILE__), '../../templates/qbrick/', self.class.name.demodulize.underscore)
  ]
end