-
Notifications
You must be signed in to change notification settings - Fork 60
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
Comments
Hmm, I don't see any benefit here. I can prepare a PR where column name will be dynamic. But with Or do you think it should allow only one 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 |
Hi simi, I don't follow what you mean by 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. 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 |
Let me prepare something. |
@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 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 |
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. |
@simi I think we can have both, many gems do. |
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. |
@johnnyshields It will be nice to refactor Would you like to do that? I think I can do that in few days. |
@simi I'm pretty slammed with work this month, so will have to pass. |
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. |
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
The text was updated successfully, but these errors were encountered: