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

allow #deleted_at attributed to be modified with a #store_as #27

Open
ak47 opened this issue Dec 8, 2014 · 11 comments
Open

allow #deleted_at attributed to be modified with a #store_as #27

ak47 opened this issue Dec 8, 2014 · 11 comments
Assignees

Comments

@ak47
Copy link
Contributor

ak47 commented Dec 8, 2014

I'm working on a PR for this now, but wondering if there was a reason this was never done in the first place. Maybe nobody has needed it yet.

We do not want to use #deleted_at as the attribute name. We are being asked instead to store the attribute as:

field :deltdDttmUtc, as: :deleted_at, type: Time

the idea is clear, but implementing is a challenge as the field method gets called from the included block

@simi
Copy link
Owner

simi commented Dec 8, 2014

Hmm, I don't see any benefit here. I can prepare a PR where column name will be dynamic. But with store_as functionality we will need to handle multiple deleted_at columns at once per document and also typecasting to correct column type.

Or do you think it should allow only one deleted_at per document and also it should force correct column type? And just use common mongoid field entry?

Idea 1:

class Fish
  include Mongoid::Document
  include Mongoid::Paranoia
  paranoid_field :deltdDttmUtc
end

Idea 2a:

class Fish
  include Mongoid::Document
  include Mongoid::Paranoia
  field :deltdDttmUtc, as: :deleted_at, type: Time
end

Idea 2b (raises "multiple deleted_at columns):

class Fish
  include Mongoid::Document
  include Mongoid::Paranoia
  field :deltdDttmUtc1, as: :deleted_at, type: Time
  field :deltdDttmUtc2, as: :deleted_at, type: Time
end

Idea 2c (raises invalid deleted_at type):

class Fish
  include Mongoid::Document
  include Mongoid::Paranoia
  field :deltdDttmUtc, as: :deleted_at, type: Array
end

@ak47
Copy link
Contributor Author

ak47 commented Dec 8, 2014

Hi simi,

I don't follow what you mean by store_as functionality.

But, yes to both of:

"Or do you think it should allow only one deleted_at per document and also it should force correct column type? And just use common mongoid field entry?"

I have some proof-of-concept code that makes it work for me.
However, I don't like the syntax it needs - due to how the deleted_at field is added in the included block.

I like your Idea 1 syntax best.

Here is the gist of my working idea:

    included do
      class_attribute :paranoia_field

      if @paranoia_field
        self.paranoia_field = @paranoia_field
        field @paranoia_field, as: :deleted_at, type: Time
      else
        field :deleted_at, type: Time
      end

      self.paranoid = true

      default_scope -> { where(deleted_at: nil) }
      scope :deleted, -> { ne(deleted_at: nil) }
      define_model_callbacks :restore
    end

    def paranoid_field
      field = self.class.paranoia_field.nil? ? 'deleted_at' : self.class.paranoia_field
      embedded? ? "#{atomic_position}.#{field}" : field
    end

but to get it to work, the include is:

# provide alternate field to contain deleted_at timestamp
@paranoia_field = 'deltdDttmUtc'
include Mongoid::Paranoia

thanks for your fast reply

@simi simi self-assigned this Dec 8, 2014
@simi
Copy link
Owner

simi commented Dec 8, 2014

Let me prepare something.

@simi
Copy link
Owner

simi commented Dec 9, 2014

@ak47 I spent some time with this. It is not impossible to implement, but it will need some refactoring.

Isn't alias_attribtute enough for you for now? I tested it without problems. In document structure, there will be deleted_at, but you can use your custom column name via document public API.

class Fish
  include Mongoid::Document
  include Mongoid::Paranoia

  alias_attribute :customDeleted, :deleted_at
end

fish = Fish.create
fish.destroy
fish.customDeleted #=> 2014-12-09 05:05:27 +0100
expect(fish.customDeleted).to eq(fish.deleted_at) // pass

@johnnyshields
Copy link
Collaborator

@simi the main idea here is to change the Mongoid field name where deleted_at is stored. This seems like a reasonable idea to do me.

@ak47 what about first doing a global config, something like:

Mongoid::Paranoia.config do |c|
  c.field = :myFieldName
end

@simi
Copy link
Owner

simi commented Dec 9, 2014

I got it @johnnyshields I'm just asking if it is enough for now. I'm rethinking how to do this. But I don't think global config will be good solution since it makes more sense to be able to specify this on per-model basis.

@johnnyshields
Copy link
Collaborator

@simi I think we can have both, many gems do.

@simi
Copy link
Owner

simi commented Dec 9, 2014

The global config is not problem to add now. But I think it should be very dangerous to use. Anyway feel free to add it if you want now. I'm still thinking about per-model config.

@simi
Copy link
Owner

simi commented Dec 9, 2014

@johnnyshields It will be nice to refactor :deleted_at and "deleted_at" from whole lib into some method or variable at first.

Would you like to do that? I think I can do that in few days.

@johnnyshields
Copy link
Collaborator

@simi I'm pretty slammed with work this month, so will have to pass.

@ak47
Copy link
Contributor Author

ak47 commented Dec 9, 2014

I like the idea of global config and the per model option. In my case the global config is appealing as I will always use the same alternate key. The downside would be the global config is outside the model code.

For our needs we don't want the superfluous deleted_at field along with the the alternate. With my proof of concept code I was avoiding a big refactor but it appears that is needed. I can use my branch code for now, we have a few weeks before this goes to production.

I'm happy to help out, unless you think you'll be faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants