neoid-gem / neoid

Extend Ruby on Rails ActiveRecord with Neo4j nodes. Keep RDBMS and utilize the power of Neo4j queries.
MIT License
171 stars 33 forks source link

Make neoidable conditional? #13

Closed cobraextreme closed 11 years ago

cobraextreme commented 11 years ago

I am interested in having the neoidable configuration be conditional, so that for example a node is only created/updated/deleted given some condition on a model field. I'd be happy to give it a whirl at some point if this makes sense.

elado commented 11 years ago

Can you give an example? Will neoidable auto_index: false (like Sunspot's) + manual record.neo_save make sense?

cobraextreme commented 11 years ago

Sure, my example use case is that I have some records that have to be approved by an admin before being visible. I'd like it if there were a way to tell neoid to only make a node for those records when approved? = true. Right now I have nodes for all records and just filter out the unapproved ones later when querying neo. It works but feels a little dirty.

I'm kind of imagining something like this:

neoidable do |c|
    c.field :name
    c.create_node_when {|model| model.approved?}
  end

Or just c.neo_when(true)?

I'm not sure that doing a manual record.neo_save would be an easy way to accomplish this, but maybe I'm thinking about it wrong.

elado commented 11 years ago

I think that a better approach is "disable all auto-node creation unless i tell you"

Then you'll be able to

class Seomthing < ActiveRecord::Base
  include Neoid...

  neoidable auto_index: false, ...

  after_save :save_neo_node_if_needed

  def create_neo_node_if_needed
    neo_save if approved?
  end
end

I'll add it to the todo list.

cobraextreme commented 11 years ago

I see what you mean. That would work great for me.

elado commented 11 years ago

See latest changes. v0.1.1 supports auto_index: false.

cobraextreme commented 11 years ago

Awesome, thanks! This is really helpful.