Class: RedmineExtensions::EntityGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/redmine_extensions/entity/entity_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ EntityGenerator

Returns a new instance of EntityGenerator.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 26

def initialize(*args)
  super
  @plugin_name_underscored = plugin_name.underscore
  @plugin_pretty_name = @plugin_name_underscored.titleize
  @plugin_path = "plugins/#{@plugin_name_underscored}"
  @plugin_title = @plugin_name_underscored.camelize

  @model_name_underscored = model_name.underscore
  @model_name_pluralize_underscored = model_name.pluralize.underscore
  @controller_class = model_name.pluralize

  prepare_columns
end

Instance Attribute Details

#controller_classObject (readonly)

Returns the value of attribute controller_class.



23
24
25
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 23

def controller_class
  @controller_class
end

#db_columnsObject (readonly)

Returns the value of attribute db_columns.



24
25
26
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 24

def db_columns
  @db_columns
end

#model_name_pluralize_underscoredObject (readonly)

Returns the value of attribute model_name_pluralize_underscored.



23
24
25
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 23

def model_name_pluralize_underscored
  @model_name_pluralize_underscored
end

#model_name_underscoredObject (readonly)

Returns the value of attribute model_name_underscored.



23
24
25
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 23

def model_name_underscored
  @model_name_underscored
end

#plugin_name_underscoredObject (readonly)

Returns the value of attribute plugin_name_underscored.



22
23
24
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 22

def plugin_name_underscored
  @plugin_name_underscored
end

#plugin_pathObject (readonly)

Returns the value of attribute plugin_path.



22
23
24
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 22

def plugin_path
  @plugin_path
end

#plugin_pretty_nameObject (readonly)

Returns the value of attribute plugin_pretty_name.



22
23
24
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 22

def plugin_pretty_name
  @plugin_pretty_name
end

#plugin_titleObject (readonly)

Returns the value of attribute plugin_title.



22
23
24
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 22

def plugin_title
  @plugin_title
end

Instance Method Details

#copy_templatesObject



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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 40

def copy_templates
  template '_form.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/_form.html.erb"
  template '_sidebar.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/_sidebar.html.erb"
  template '_view_custom_fields_form_custom_field.html.erb.erb', "#{plugin_path}/app/views/custom_fields/_view_custom_fields_form_#{model_name_underscored}_custom_field.html.erb"
  template 'context_menu.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/context_menu.html.erb"
  template 'controller.rb.erb', "#{plugin_path}/app/controllers/#{model_name_pluralize_underscored}_controller.rb"
  template('custom_field.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}_custom_field.rb") if acts_as_customizable?
  template 'edit.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/edit.html.erb"

  if File.exists?("#{plugin_path}/config/locales/en.yml")
    append_to_file "#{plugin_path}/config/locales/en.yml" do
      "\n  activerecord:" +
        "\n  easy_query:" +
        "\n    attributes:" +
        "\n      #{model_name_underscored}:" +
        db_columns.collect { |column_name, column_options| "\n        #{column_options[:lang_key]}: #{column_name.humanize}" }.join +
        "\n    name:" +
        "\n      #{model_name_underscored}_query: #{model_name_pluralize_underscored.titleize}" +
        "\n  heading_#{model_name_underscored}_new: New #{model_name_underscored.titleize}" +
        "\n  heading_#{model_name_underscored}_edit: Edit #{model_name_underscored.titleize}" +
        "\n  button_#{model_name_underscored}_new: New #{model_name_underscored.titleize}" +
        "\n  label_#{model_name_pluralize_underscored}: #{@model_name_pluralize_underscored.titleize}" +
        "\n  label_#{model_name_underscored}: #{model_name_underscored.titleize}" +
        "\n  permission_view_#{model_name_pluralize_underscored}: View #{model_name_pluralize_underscored.titleize}" +
        "\n  permission_manage_#{model_name_pluralize_underscored}: Manage #{model_name_pluralize_underscored.titleize}" +
        "\n  title_#{model_name_underscored}_new: Click to create new #{model_name_underscored.titleize}"
    end
  else
    template 'en.yml.erb', "#{plugin_path}/config/locales/en.yml"
  end

  template 'helper.rb.erb', "#{plugin_path}/app/helpers/#{model_name_pluralize_underscored}_helper.rb"
  template 'hooks.rb.erb', "#{plugin_path}/lib/#{plugin_name_underscored}/#{model_name_underscored}_hooks.rb"
  template 'index.api.rsb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/index.api.rsb"
  template 'index.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/index.html.erb"

  if mail?
    template 'mailer.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}_mailer.rb"
    template 'mail_added.html.erb.erb', "#{plugin_path}/app/views/#{model_name_underscored}_mailer/#{model_name_underscored}_added.html.erb"
    template 'mail_added.text.erb.erb', "#{plugin_path}/app/views/#{model_name_underscored}_mailer/#{model_name_underscored}_added.text.erb"
    template 'mail_updated.html.erb.erb', "#{plugin_path}/app/views/#{model_name_underscored}_mailer/#{model_name_underscored}_updated.html.erb"
    template 'mail_updated.text.erb.erb', "#{plugin_path}/app/views/#{model_name_underscored}_mailer/#{model_name_underscored}_updated.text.erb"
  end

  template 'migration.rb.erb', "#{plugin_path}/db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_create_#{@model_name_pluralize_underscored}.rb"
  template 'model.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}.rb"
  template 'new.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/new.html.erb"
  template 'query.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}_query.rb"

  if File.exists?("#{plugin_path}/config/routes.rb")
    if project?
      append_to_file "#{plugin_path}/config/routes.rb" do
        "\nresources :projects do " +
          "\n  resources :#{model_name_pluralize_underscored}" +
          "\nend\n"
      end
    end
    append_to_file "#{plugin_path}/config/routes.rb" do
      "\nresources :#{model_name_pluralize_underscored} do" +
        "\n  collection do " +
        "\n    get 'autocomplete'" +
        "\n    get 'bulk_edit'" +
        "\n    post 'bulk_update'" +
        "\n    get 'context_menu'" +
        "\n  end" +
        "\nend"
    end
  else
    template 'routes.rb.erb', "#{plugin_path}/config/routes.rb"
  end

  if File.exists?("#{plugin_path}/init.rb")
    s = "\nActiveSupport.on_load(:easyproject, yield: true) do"
    s << "\n  require '#{plugin_name_underscored}/#{model_name_underscored}_hooks'\n"
    s << "\n  Redmine::AccessControl.map do |map|"
    s << "\n    map.project_module :#{model_name_pluralize_underscored} do |pmap|"
    s << "\n      pmap.permission :view_#{model_name_pluralize_underscored}, { #{model_name_pluralize_underscored}: [:index, :show, :autocomplete, :context_menu] }, read: true"
    s << "\n      pmap.permission :manage_#{model_name_pluralize_underscored}, { #{model_name_pluralize_underscored}: [:new, :create, :edit, :update, :destroy, :bulk_edit, :bulk_update] }"
    s << "\n    end "
    s << "\n  end\n"
    s << "\n  Redmine::MenuManager.map :top_menu do |menu|"
    s << "\n    menu.push :#{model_name_pluralize_underscored}, { controller: '#{model_name_pluralize_underscored}', action: 'index', project_id: nil }, caption: :label_#{model_name_pluralize_underscored}"
    s << "\n  end\n"
    if project?
      s << "\n  Redmine::MenuManager.map :project_menu do |menu|"
      s << "\n    menu.push :#{model_name_pluralize_underscored}, { controller: '#{model_name_pluralize_underscored}', action: 'index' }, param: :project_id, caption: :label_#{model_name_pluralize_underscored}"
      s << "\n  end\n"
    end
    if acts_as_customizable?
      s << "\n  CustomFieldsHelper::CUSTOM_FIELDS_TABS << {name: '#{model_name}CustomField', partial: 'custom_fields/index', label: :label_#{model_name_pluralize_underscored}}\n"
    end
    if acts_as_searchable?
      s << "\n  Redmine::Search.map do |search|"
      s << "\n    search.register :#{model_name_pluralize_underscored}"
      s << "\n  end\n"
    end
    if acts_as_activity_provider?
      s << "\n  Redmine::Activity.map do |activity|"
      s << "\n    activity.register :#{model_name_pluralize_underscored}, {class_name: %w(#{model_name}), default: false}"
      s << "\n  end\n"
    end
    s << "\nend"

    append_to_file "#{plugin_path}/init.rb", s
  end


  template 'show.api.rsb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/show.api.rsb"
  template 'show.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/show.html.erb"
end