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.



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

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

#associationsObject (readonly)

Returns the value of attribute associations.



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

def associations
  @associations
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



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

def controller_class
  @controller_class
end

#db_columnsObject (readonly)

Returns the value of attribute db_columns.



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

def db_columns
  @db_columns
end

#model_name_pluralize_underscoredObject (readonly)

Returns the value of attribute model_name_pluralize_underscored.



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

def model_name_pluralize_underscored
  @model_name_pluralize_underscored
end

#model_name_underscoredObject (readonly)

Returns the value of attribute model_name_underscored.



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

def model_name_underscored
  @model_name_underscored
end

#plugin_name_underscoredObject (readonly)

Returns the value of attribute plugin_name_underscored.



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

def plugin_name_underscored
  @plugin_name_underscored
end

#plugin_pathObject (readonly)

Returns the value of attribute plugin_path.



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

def plugin_path
  @plugin_path
end

#plugin_pretty_nameObject (readonly)

Returns the value of attribute plugin_pretty_name.



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

def plugin_pretty_name
  @plugin_pretty_name
end

#plugin_titleObject (readonly)

Returns the value of attribute plugin_title.



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

def plugin_title
  @plugin_title
end

Instance Method Details

#copy_templatesObject



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
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
# File 'lib/generators/redmine_extensions/entity/entity_generator.rb', line 43

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"

  template 'controller_spec.rb.erb', "#{plugin_path}/test/spec/controllers/#{model_name_pluralize_underscored}_controller_spec.rb"
  template 'factories.rb.erb', "#{plugin_path}/test/factories/#{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"

  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

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