Module: Rabarber::MigrationHelpers

Defined in:
lib/rabarber/helpers/migration_helpers.rb

Instance Method Summary collapse

Instance Method Details

#delete_authorization_context!(context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rabarber/helpers/migration_helpers.rb', line 14

def delete_authorization_context!(context)
  roles = Rabarber::Role.where(context_type: context.to_s)

  raise Rabarber::InvalidArgumentError, "No roles exist in context #{context.inspect}" unless roles.exists?

  ActiveRecord::Base.transaction do
    ActiveRecord::Base.connection.execute(
      ActiveRecord::Base.sanitize_sql(
        ["DELETE FROM rabarber_roles_roleables WHERE role_id IN (?)", roles.pluck(:id)]
      )
    )
    roles.delete_all
  end
end

#migrate_authorization_context!(old_context, new_context) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/rabarber/helpers/migration_helpers.rb', line 5

def migrate_authorization_context!(old_context, new_context)
  roles = Rabarber::Role.where(context_type: old_context.to_s)

  raise Rabarber::InvalidArgumentError, "No roles exist in context #{old_context.inspect}" unless roles.exists?
  raise Rabarber::InvalidArgumentError, "Cannot migrate context to #{new_context.inspect}: class does not exist" unless new_context.to_s.safe_constantize

  roles.update_all(context_type: new_context.to_s)
end