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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 31

def initialize(*args)
  super
  @rys = !!options["rys"]
  @plugin_name_underscored = plugin_name.underscore
  @plugin_pretty_name = @plugin_name_underscored.titleize

  @plugin_path = rys ? "." : "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

#associationsObject (readonly)

Returns the value of attribute associations.



29
30
31
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 29

def associations
  @associations
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



27
28
29
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 27

def controller_class
  @controller_class
end

#db_columnsObject (readonly)

Returns the value of attribute db_columns.



28
29
30
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 28

def db_columns
  @db_columns
end

#model_name_pluralize_underscoredObject (readonly)

Returns the value of attribute model_name_pluralize_underscored.



27
28
29
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 27

def model_name_pluralize_underscored
  @model_name_pluralize_underscored
end

#model_name_underscoredObject (readonly)

Returns the value of attribute model_name_underscored.



27
28
29
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 27

def model_name_underscored
  @model_name_underscored
end

#plugin_name_underscoredObject (readonly)

Returns the value of attribute plugin_name_underscored.



26
27
28
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 26

def plugin_name_underscored
  @plugin_name_underscored
end

#plugin_pathObject (readonly)

Returns the value of attribute plugin_path.



26
27
28
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 26

def plugin_path
  @plugin_path
end

#plugin_pretty_nameObject (readonly)

Returns the value of attribute plugin_pretty_name.



26
27
28
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 26

def plugin_pretty_name
  @plugin_pretty_name
end

#plugin_titleObject (readonly)

Returns the value of attribute plugin_title.



26
27
28
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 26

def plugin_title
  @plugin_title
end

#rysObject (readonly)

Returns the value of attribute rys.



30
31
32
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 30

def rys
  @rys
end

Instance Method Details

#copy_templatesObject



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 47

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 '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"
  template 'edit.js.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/edit.js.erb"

  tests_root = rys ? "./spec" : "#{plugin_path}/test"
  template 'controller_spec.rb.erb', "#{tests_root + (!rys && "/spec" || "")}/controllers/#{model_name_pluralize_underscored}_controller_spec.rb"
  template 'factories.rb.erb', "#{tests_root}/factories/#{model_name_underscored}.rb"
  template 'spec/requests/%model_name_underscored%_spec.rb.tt', "#{tests_root}/requests/#{model_name_underscored}.rb"

  if File.exists?("#{plugin_path}/config/locales/en.yml")
    original_langfile = YAML.load_file("#{plugin_path}/config/locales/en.yml")

    template 'en.yml.erb', "#{plugin_path}/tmp/tmp_en.yml", pretend: true
    if File.exist?("#{plugin_path}/tmp/tmp_en.yml")
      added_translations = YAML.load_file("#{plugin_path}/tmp/tmp_en.yml")
      File.delete("#{plugin_path}/tmp/tmp_en.yml")

      merged_langfile = original_langfile.deep_merge(added_translations)
      File.open("#{plugin_path}/config/locales/en.yml", "w") do |file|
        file.write merged_langfile.to_yaml
      end
    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"
  template 'index.js.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/index.js.erb"
  template '_list.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/_list.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%L')}_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 'new.js.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/new.js.erb"

  if Redmine::Plugin.installed?(:easy_extensions)
    template 'easy_query.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}_query.rb"
    template 'entity_attribute_helper_patch.rb.erb', "#{plugin_path}/extra/easy_patch/easy_extensions/helpers/entity_attribute_helper_patch.rb"

    if File.exists?("#{plugin_path}/extra/easy_patch/easy_extensions/helpers/entity_attribute_helper_patch.rb")

      inject_into_file "#{plugin_path}/extra/easy_patch/easy_extensions/helpers/entity_attribute_helper_patch.rb",
                       "\n       def format_html_#{model_name_underscored}_attribute(entity_class, attribute, unformatted_value, options={})" +
                         "\n          value = format_entity_attribute(entity_class, attribute, unformatted_value, options)" +
                         "\n          case attribute.name" +
                         "\n          when :name" +
                         "\n            link_to(value, #{model_name_underscored.singularize}_path(options[:entity].id))" +
                         "\n          else" +
                         "\n            h(value)" +
                         "\n          end" +
                         "\n        end" +
                         "\n", after: "base.class_eval do"

    end
  else
    template 'query.rb.erb', "#{plugin_path}/app/models/#{model_name_underscored}_query.rb"
  end

  routes_file = "#{plugin_path}/config/routes.rb"
  if File.exists?(routes_file)
    if project?
      insert_into_file routes_file, before: (rys ? /end\z/ : /\z/) do
        "\nresources :projects do " +
          "\n  resources :#{model_name_pluralize_underscored}" +
          "\nend\n"
      end
    end
    insert_into_file routes_file, before: (rys ? /end\z/ : /\z/) 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}/after_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}/after_init.rb", s
  elsif rys
    s = ""
    s << "\n  Redmine::AccessControl.map do |map|"
    s << "\n    map.easy_category :#{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"
    append_to_file "./config/initializers/01_access_control.rb", s

    s = ""
    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
    append_to_file "./config/initializers/02_menu_manager.rb", s
    s = ""
    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
    other_initializer = "./config/initializers/06_others.rb"
    if File.exist?(other_initializer)
      append_file other_initializer do
        s
      end
    else
      create_file other_initializer do
        s
      end
    end

  end


  template 'show.api.rsb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/show.api.rsb"
  template '_%model_name_underscored%.api.rsb.tt', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/_#{model_name_underscored}.api.rsb"
  template 'show.html.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/show.html.erb"
  template 'show.js.erb.erb', "#{plugin_path}/app/views/#{model_name_pluralize_underscored}/show.js.erb"
end