GithubHelp home page GithubHelp logo

Comments (13)

Adeering avatar Adeering commented on July 4, 2024 1

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

from logidze.

Adeering avatar Adeering commented on July 4, 2024

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

from logidze.

charlie-wasp avatar charlie-wasp commented on July 4, 2024

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

from logidze.

Adeering avatar Adeering commented on July 4, 2024

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

from logidze.

charlie-wasp avatar charlie-wasp commented on July 4, 2024

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

from logidze.

Adeering avatar Adeering commented on July 4, 2024

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

from logidze.

charlie-wasp avatar charlie-wasp commented on July 4, 2024

@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

from logidze.

Adeering avatar Adeering commented on July 4, 2024

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

from logidze.

charlie-wasp avatar charlie-wasp commented on July 4, 2024

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?

from logidze.

palkan avatar palkan commented on July 4, 2024

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?

from logidze.

Adeering avatar Adeering commented on July 4, 2024

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?

from logidze.

palkan avatar palkan commented on July 4, 2024

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)

from logidze.

palkan avatar palkan commented on July 4, 2024

Glad to help you) Closing the issue.

from logidze.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.