maxrossello / redmine_app_timesheets

A true timesheet plugin using orders, not bound to timelogs over issues
GNU General Public License v2.0
32 stars 10 forks source link

Saving plugin settings looses custom field setting #38

Closed KarstenWolff closed 9 years ago

KarstenWolff commented 9 years ago

At the time being the I have installed only the plugins redmine_app__space and redmine_app_timesheets.

My test project has two versions named PG-Milestone1 and PG-Milestone2. For PG-Milestone1 field Is Order is set to Yes and for PG-Milestone2 it is set to No. In the web browser this is shown correct. The values in table _customvalues are correct.

But the field _isorder in table versions is always false for these local versions. See output of tables below.

My database is (now) PostgreSql.

redmine=> select id, project_id, name, status, sharing, in_timesheet, is_order from versions;
 id | project_id |       name       | status | sharing | in_timesheet | is_order
----+------------+------------------+--------+---------+--------------+----------
  3 |          2 | PG1-Milestone1   | open   | none    | f            | f
  4 |          2 | PG1-Milestone2   | open   | none    | f            | f
  2 |          4 | Vacation (payed) | open   | system  | t            | t
  1 |          4 | Sick leave       | open   | system  | t            | t
(4 rows)

redmine=> select id, type, name, field_format from custom_fields;
 id |        type        |   name   | field_format
----+--------------------+----------+--------------
  1 | VersionCustomField | Is order | bool
(1 row)

redmine=> select * from custom_values;
 id | customized_type | customized_id | custom_field_id | value
----+-----------------+---------------+-----------------+-------
  4 | Version         |             4 |               1 | 0
  3 | Version         |             3 |               1 | 1
(2 rows)

redmine=>

In PostgreSql I enabled the logging of SQL statements.

The Orders application shows "Available orders and workspace versions" on the right side. One of the executed queries is:

SELECT "versions".* FROM "versions" WHERE "versions"."is_order" = 't';

The query does not use the value from table custom_values as it is done when Versions are edited in the settings dialog of a project. In this dialog the Save action does only do an update on table custom_fields, not on versions.

For version PG-Milestone1 the only UPDATE statement when setting Is Order to Yes is this one:

UPDATE "custom_values" SET "value" = '1' WHERE "custom_values"."id" = 3

I guess the query in the Orders application should either use table custom_fields, too, or the column versions.is_order must be updated as well, when table custom_values is updated.

maxrossello commented 9 years ago

Hi, what you mention is actually the expected behaviour. If you have a look at file lib/timesheets_app_version_patch.rb, you can see the following section:

def validate_order
  custom_field_values.each do |cv|
    if cv.custom_field_id == Setting.plugin_redmine_app_timesheets["field"]
      if TimeEntry.where(:order_id => self.id).any?  # timelogs associated
        self.is_order = true
        self.in_timesheet = cv.value
      else
        self.is_order = cv.value
      end

      return false if (self.project_id == Setting.plugin_redmine_app_timesheets['project'].to_i and self.is_order == false)
    end
  end
  if self.is_order and changed_attributes['in_timesheet'].nil?
    self.in_timesheet = true
  end
  true
end

The above code should run at Version save, and the meaning of its first section is: if we have timelogs attached, setting the Is order custom field as No does not set is_order to false, rather just sets _intimesheet as false (disable the Order and list it in the right side of the Orders page). Otherwise, _isorder is set as the value of the custom field:

self.is_order = cv.value

May you try to set some logs there, looking why the above code is not running, or is running with unexpected data? Something like:

Rails.logger.info 'I am here...'
KarstenWolff commented 9 years ago

I did a bit debugging... I think the problem is that in lib/timesheets_app_version_patch.rb the expression Setting.plugin_redmine_app_timesheets["field"] was nil for me.

For a test I changed the database entry. I added field: 1 to the hash table:

redmine=> update settings set value = e'--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nproject: ''4''\npublic_versions: ''1''\nfield: 1\n' WHERE id = 30;
UPDATE 1
redmine=> select id, name, value from settings where name = 'plugin_redmine_app_timesheets';                                                 id |             name              |                          value
----+-------------------------------+---------------------------------------------------------
 30 | plugin_redmine_app_timesheets | --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess+
    |                               | project: '4'                                           +
    |                               | public_versions: '1'                                   +
    |                               | field: 1                                               +
    |                               |
(1 row)

redmine=> commit;

After a restart of redmine and saving the version 'PG1-Milestone1' with Is Order = Yes, the fields is_order and in_timesheet were updated (both values were false before):

redmine=> select id, project_id, name, status, sharing, in_timesheet, is_order from versions;
 id | project_id |       name       | status | sharing | in_timesheet | is_order
----+------------+------------------+--------+---------+--------------+----------
  4 |          2 | PG1-Milestone2   | open   | none    | f            | f
  2 |          4 | Vacation (payed) | open   | system  | t            | t
  1 |          4 | Sick leave       | open   | system  | t            | t
  3 |          2 | PG1-Milestone1   | open   | none    | t            | t
(4 rows)

And now this version/order is shown as enabled on the left side in the order application - as expected.

Then I rolled back my virtual machine and installed the plugin redmine_app_timesheets again. After RAILS_ENV=production rake redmine:plugins:migrate the setting in the database include the field => 1 in the hash table. That's ok.

redmine=> select id, name, value from settings where name = 'plugin_redmine_app_timesheets';
 id |             name              |         value
----+-------------------------------+-----------------------
 31 | plugin_redmine_app_timesheets | ---                  +
    |                               | project: ''          +
    |                               | public_versions: true+
    |                               | field: 1             +
    |                               |
(1 row)

The next step was the configuration of the plugin. I created a project Timesheets and used this as the backing workspace. Shared versions visible to non members was checked.

Then I applied the change. The executed SQL statement was:

UPDATE "settings" SET "value" = '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
        project: ''4''
        public_versions: ''1''
        ', "updated_on" = '2014-12-05 16:20:00.921525' WHERE "settings"."id" = 31

The entry field: 1 in the hash table was gone. I think that is the problem. I couldn't find the code which is is executed when you press Apply, but I'm sure you will know. I'm not (yet) a Ruby nor aRails developer :smile:

maxrossello commented 9 years ago

Hmmmm... what a lapse of mine...! Yes, it's all clear now. I think I can make a patch in the weekend

Thank you!