-
-
Save kivanio/05f775af7d250cf36ad3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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