Project

General

Profile

How add an attribute in users groups with plugin

Added by Francesco Chimi about 3 hours ago

Hi,
I need to add a field via plugin to groups.

I've read the documentation about plugin, but it seems not present this information.
I've already searched a lot online with no results.

Situation:
  • the field is present in db table users
  • the field is present in view
  • the field is correctly loaded by db (I've change a value via sql update)
  • But when I change the value and click the Save button, the field reverts to its original value.

This is the migration file:

class AddAuthorVisibilityToGroups < ActiveRecord::Migration[6.1]
  def up
    add_column :users, :author_visibility, :integer , default: 0
  end

  def down
    remove_column :users, :author_visibility
  end
end

The ./app/views/groups/_form.html.erb file is a copy of the core file with this code added to the bottom:

<div>
  <p>
    <%= f.label :author_visibility, l(:label_author_visibility) %><br />
    <%= f.select :author_visibility,
        options_for_select([
        [l(:label_author_visibility_no), 0],
        [l(:label_author_visibility_only_members), 1]
       ], selected: @group.author_visibility),
          include_blank: false %>
  </p>
</div>

This is the lib/redmine_author_visibility_to_groups/GroupPath file:

module RedmineAuthorVisibilityToGroups
  module GroupPatch
    def self.included(base)
      base.class_eval do
        # se vuoi enum interno
        enum author_visibility: { no: 0, only_members: 1 }

        # enable massive assignment
        safe_attributes 'author_visibility'
      end
    end
  end
end

Group.prepend RedmineAuthorVisibilityToGroups::GroupPatch

this is the init.rb file:

require_relative 'lib/redmine_author_visibility_to_groups/group_patch'

Redmine::Plugin.register :plg_test1 do
  name 'Plg Test1 plugin'
  author 'zzz'
  description 'test1 (plugin for Redmine)'
  version '0.0.1'
  url 'http://example.com/path/to/plugin'
  author_url 'http://example.com/about'
end

Rails.configuration.to_prepare do
  Group.include RedmineAuthorVisibilityToGroups::GroupPatch
end

This is info environment:

Environment:
  Redmine version                6.1.1.stable
  Ruby version                   3.2.3-p157 (2024-01-18) [x86_64-linux]
  Rails version                  7.2.3
  Environment                    production
  Database adapter               PostgreSQL
  Mailer queue                   ActiveJob::QueueAdapters::AsyncAdapter
  Mailer delivery                smtp
Redmine settings:
  Redmine theme                  Default
SCM:
  Subversion                     1.14.1
  Git                            2.34.1
  Filesystem                     

What am I doing wrong?
Thanks in advance for any help!