Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from gvalmon/has_bulk_actions.rb
Created June 23, 2014 00:27
Show Gist options
  • Save kivanio/05f775af7d250cf36ad3 to your computer and use it in GitHub Desktop.
Save kivanio/05f775af7d250cf36ad3 to your computer and use it in GitHub Desktop.
module HasBulkActions
extend ActiveSupport::Concern
module ClassMethods
def bulk_update(ids, attributes, params = {})
where("#{table_name}.id" => ids).update_all(attributes)
extract_bulk_events(params[:events], attributes).each do |event|
notify(event.to_sym)
end
end
def bulk_update_associations(ids, attributes, associations = [])
tables = associations.map do |association|
extract_associated_table(association)
end
bulk_update_tables(ids, attributes, tables)
end
private
def bulk_update_tables(ids, attributes, tables)
# "UPDATE #{tables.split(',')} WHERE ..."
end
def extract_bulk_events(events, attributes)
events ? events : attributes.keys.map {|key| "bulk_update_#{key}" }
end
def extract_associated_table(association)
reflection = reflect_on_all_associations.detect do |reflection|
reflection.name.to_s == association.to_s
end
reflection ? reflection.options[:active_record].table_name : nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment