mod/history/set/all/content_history.rb in card-1.91 vs mod/history/set/all/content_history.rb in card-1.92

- old
+ new

@@ -26,16 +26,27 @@ .card_changes.build(field: :db_content, value: content) end end def last_change_on field, opts={} - Change.joins(:action).where( - last_change_sql_conditions(opts), - card_id: id, - action_id: extract_action_id(opts[:before] || opts[:not_after]), - field: Card::Change.field_index(field) - ).order(:id).last + action_id = extract_action_id(opts[:before] || opts[:not_after]) + + # If there is only one action then there are no entries in the changes table, + # so we can't do a sql search but the changes are accessible via the action. + if action_id == create_action.id + return if opts[:before] # there is no before the first action + create_action.change field + elsif !action_id && create_action.sole? + create_action.change field + else + Change.joins(:action).where( + last_change_sql_conditions(opts), + card_id: id, + action_id: action_id, + field: Card::Change.field_index(field) + ).order(:id).last + end end def extract_action_id action_arg action_arg.is_a?(Card::Action) ? action_arg.id : action_arg end @@ -85,29 +96,27 @@ return unless @current_action && current_action_changes_content? @current_action.id end def current_action_changes_content? - new_card? || @current_action.new_content? || db_content_changed? + new_card? || @current_action.new_content? || db_content_is_changing? end def last_action_id - (la = last_action) && la.id + last_action&.id end def last_action actions.where("id IS NOT NULL").last end def last_content_action - l_c = last_change_on :db_content - l_c && l_c.action + last_change_on(:db_content)&.action end def last_content_action_id - l_c = last_change_on :db_content - l_c && l_c.card_action_id + last_change_on(:db_content)&.card_action_id end def last_actor last_act.actor end @@ -157,11 +166,10 @@ def draft_acts drafts.created_by(Card::Auth.current_id).map(&:act) end -event :detect_conflict, :validate, on: :update, - when: proc { |c| c.edit_conflict? } do +event :detect_conflict, :validate, on: :update, when: :edit_conflict? do errors.add :conflict, "changes not based on latest revision" end def edit_conflict? last_action_id_before_edit &&