Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to turn on Associations versioning #45

Closed
Adeering opened this issue Aug 19, 2017 · 13 comments
Closed

How to turn on Associations versioning #45

Adeering opened this issue Aug 19, 2017 · 13 comments
Assignees
Labels
awaiting response Awaiting reporter's response

Comments

@Adeering
Copy link

So I saw in the wiki that association versioning is available and I need that feature. It has in there this:

This feature is considered experimental, so it's disabled by default. You can turn it on by setting Logidze.associations_versioning to true. Associations versioning works with belongs_to and has_many associations

Where do I turn on the associations_versioning setting and set it to true?

@palkan palkan added the awaiting response Awaiting reporter's response label Aug 19, 2017
@Adeering
Copy link
Author

Any update to this, was trying to switch from papertrail and would like to get this started

@charlie-wasp
Copy link
Collaborator

@Adeering Hi! You can create a file config/initializers/logidze.rb and put Logidze.associations_versioning = true there

@Adeering
Copy link
Author

Ok so I created the file and turned it on, but it doesnt seem to be working. Both models have the has_logidze and independently they log the changes. I have a recipe table and a recipe_ingredient table. The recipe has_many recipe_ingredients. When I update a recipe_ingredient it saves the update in the log, but the log count in the recipe log_data doesnt change

@charlie-wasp
Copy link
Collaborator

@Adeering can you please provide actual code, that you running? With the notes, what results are you expecting, and what you actually get

@Adeering
Copy link
Author

Sure here is the code:
My Recipe Model:

class Recipe < ApplicationRecord
  	has_logidze
	#sets a lot of ingredients to 1 recipe
	has_many :recipe_ingredients, inverse_of: :recipe, dependent: :destroy

Recipe Ingredients Model:

class RecipeIngredient < ApplicationRecord
  	has_logidze
	belongs_to :recipe

And then my recipes controller:

class RecipesController < ApplicationController
  before_action :set_recipe, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  around_action :set_logidze_responsible, only: [:create, :update]
  def set_logidze_responsible(&block)
    Logidze.with_responsible(current_user&.id, &block)
  end
  def update
    respond_to do |format|
      if @recipe.update(recipe_params)
        format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
        format.json { render :show, status: :ok, location: @recipe }
      else
        format.html { render :edit }
        format.json { render json: @recipe.errors, status: :unprocessable_entity }
      end
    end
  end

Lastly the logidze.rb in the initializers:
Logidze.associations_versioning = true

So I might be misunderstanding what is going to happen with the associated records, but expecting it to create a new version in the recipe log_data column whenever an ingredient changes. Right now it saves a new version of the ingredient in the ingredients log_data column but nothing changes in the recipe log_data column.

What im trying to do is create a view so the user can cycle thru all the changes and see what has changed. To do that I need to have a base reference as to which changes to call out for a set of changes, so was thinking that would be put in the recipes log_data column

@charlie-wasp
Copy link
Collaborator

@Adeering I am afraid, it's not, how it works.

When you change ingredients, no new log_data entries in the recipe are created. Instead, when you “rewind” the recipe to the particular time, and then access its ingredients, in that moment ingredients will be “rewinded” to the same time automagically

@Adeering
Copy link
Author

Ok, so if the only things that are changing are the ingredients, is there a way I could cycle thru and show each time there is a change? More than just 1 ingredient can change at a time also

@charlie-wasp
Copy link
Collaborator

I guess, there is no 100% reliable way to do that. You can do something like this, of course:

recipe.recipe_ingredients.each do |ingredient|
  ingredient.log_data.versions.each do |v|
    # here do something with ingredient.at(v.time)
  end
end

But this approach doesn't take into account deleted ingredients, the won't be shown

@palkan maybe, you've got some ideas here? Am I missing something?

@palkan
Copy link
Owner

palkan commented Aug 22, 2017

I have the following idea:

# Custom enumerator
recipe.each_version(includes: :recipe_ingredients) do |(timestamp, recipe_at_the_time)|
  # here recipe_at_the_time is just recipe.at(timestamp)
end

# To solve the problem of multiple changes within a short amount of time,
# we can provide a threshold option: combine changes which are less 1 minute one from each other.
recipe.each_version(include: :recipe_ingredients, threshold: 1.minute) do
  ...
end

Logidze doesn't have a special support for deleted association records. But you can combine it with paranoia gem and do something like that:

class Recipe < ApplicationRecord
  acts_as_paranoid
  # add another association that takes into account deleted records 
  has_many :recipe_ingridients_with_deleted, -> { with_deleted }
end

# and then
recipe.each_version(include: :recipe_ingredients_with_deleted) do
  ...
end

@Adeering What do you think about the idea? Does it solve your problem?

@Adeering
Copy link
Author

Ok, so I think I figured out a way to do this. So in my case Im not trying to track a normal edit from a user. What happens is users vote on changes to recipes and if they get enough votes they get applied and change the recipe. I already track how many changes get approved for my search functions....originally I blacklisted all my counter indexes, but I can whitelist the successful change count and that will trigger a version in the recipe to be created. Then use the enumerator you suggested to display them.

So new question...how can I go back and change the whitelist?

@palkan
Copy link
Owner

palkan commented Aug 22, 2017

how can I go back and change the whitelist?

Just released v0.5.3 which add support for upgrade migrations (see #46). Just run generator with --update option:

rails generate logidze:model recipe --update --whitelist=...

And run db:migrate to re-create trigger.

Then use the enumerator you suggested to display them

Just to clarify: the enumerator above is just a proposal, it's not there yet) If you would like to implement it – feel free to submit a PR)

@Adeering
Copy link
Author

ok, should be simple enough to create a loop, since you can reference each version via the version number. I think I've got a solid picture of what I need to do now. Thank you for the help

@palkan
Copy link
Owner

palkan commented Aug 23, 2017

Glad to help you) Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Awaiting reporter's response
Projects
None yet
Development

No branches or pull requests

3 participants