Skip to content

Commit

Permalink
Refactor reference to user class to something configurable by the eng…
Browse files Browse the repository at this point in the history
…ine.
  • Loading branch information
jkeck committed Dec 9, 2015
1 parent 79b4f2b commit fd181f9
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/spotlight/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Ability

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def initialize(user)
user ||= ::User.new
user ||= Spotlight::Engine.user_class.new

alias_action :process_import, to: :import

Expand Down
2 changes: 1 addition & 1 deletion app/models/spotlight/exhibit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Exhibit < ActiveRecord::Base
has_many :roles, dependent: :delete_all
has_many :searches, dependent: :destroy, extend: FriendlyId::FinderMethods
has_many :solr_document_sidecars, dependent: :delete_all
has_many :users, through: :roles, class_name: '::User'
has_many :users, through: :roles, class_name: Spotlight::Engine.config.user_class
has_many :pages, dependent: :destroy

has_one :blacklight_configuration, class_name: 'Spotlight::BlacklightConfiguration', dependent: :delete
Expand Down
4 changes: 2 additions & 2 deletions app/models/spotlight/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Page < ActiveRecord::Base
friendly_id :title, use: [:slugged, :scoped, :finders, :history], scope: :exhibit

belongs_to :exhibit, touch: true
belongs_to :created_by, class_name: '::User'
belongs_to :last_edited_by, class_name: '::User'
belongs_to :created_by, class_name: Spotlight::Engine.config.user_class
belongs_to :last_edited_by, class_name: Spotlight::Engine.config.user_class
validates :weight, inclusion: { in: proc { 0..Spotlight::Page::MAX_PAGES } }

default_scope { order('weight ASC') }
Expand Down
4 changes: 2 additions & 2 deletions app/models/spotlight/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spotlight
class Role < ActiveRecord::Base
ROLES = %w(admin curator)
belongs_to :exhibit
belongs_to :user, class_name: '::User', autosave: true
belongs_to :user, class_name: Spotlight::Engine.config.user_class, autosave: true
validates :role, inclusion: { in: ROLES }
validates :user_key, presence: true
validate :user_must_exist, if: -> { user_key.present? }
Expand All @@ -21,7 +21,7 @@ def user_key
# setting user key causes the user to get set
def user_key=(key)
@user_key = key
self.user ||= ::User.find_by_user_key(key)
self.user ||= Spotlight::Engine.user_class.find_by_user_key(key)
user.user_key = key if user
end

Expand Down
6 changes: 6 additions & 0 deletions lib/spotlight/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Engine < ::Rails::Engine
FactoryGirl.definition_file_paths << File.expand_path('../../../spec/factories', __FILE__) if defined?(FactoryGirl)
end

def self.user_class
Spotlight::Engine.config.user_class.constantize
end

def self.catalog_controller
Spotlight::Engine.config.catalog_controller_class.constantize
end
Expand All @@ -51,6 +55,8 @@ def self.blacklight_config
Spotlight::Engine.config.default_blacklight_config || catalog_controller.blacklight_config
end

Spotlight::Engine.config.user_class = '::User'

Spotlight::Engine.config.catalog_controller_class = '::CatalogController'
Spotlight::Engine.config.default_blacklight_config = nil

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/spotlight_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace :spotlight do
end

def prompt_to_create_user
User.find_or_create_by!(email: prompt_for_email) do |u|
Spotlight::Engine.user_class.find_or_create_by!(email: prompt_for_email) do |u|
puts 'User not found. Enter a password to create the user.'
u.password = prompt_for_password
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
DatabaseCleaner.start

# The first user is automatically granted admin privileges; we don't want that behavior for many of our tests
User.create email: 'initial+admin@example.com', password: 'password', password_confirmation: 'password'
Spotlight::Engine.user_class.create email: 'initial+admin@example.com', password: 'password', password_confirmation: 'password'
end

config.after do
Expand Down
8 changes: 4 additions & 4 deletions spec/views/_user_util_links.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Spotlight
end

describe 'when user is logged in' do
let(:current_user) { ::User.new }
let(:current_user) { Spotlight::Engine.user_class.new }
it 'renders the links' do
render
expect(rendered).to have_link 'Feedback'
Expand All @@ -42,7 +42,7 @@ module Spotlight
end

describe 'when user is a curator' do
let(:current_user) { ::User.new }
let(:current_user) { Spotlight::Engine.user_class.new }
before do
allow(view).to receive(:can?).with(:update, current_exhibit).and_return(false)
allow(view).to receive(:can?).with(:create, Spotlight::Exhibit).and_return(false)
Expand All @@ -56,7 +56,7 @@ module Spotlight
end
end
describe 'when user is an admin' do
let(:current_user) { ::User.new }
let(:current_user) { Spotlight::Engine.user_class.new }
before do
allow(view).to receive(:can?).with(:update, current_exhibit).and_return(true)
allow(view).to receive(:can?).with(:create, Spotlight::Exhibit).and_return(false)
Expand All @@ -71,7 +71,7 @@ module Spotlight
end

describe 'when user is a site-wide admin' do
let(:current_user) { ::User.new }
let(:current_user) { Spotlight::Engine.user_class.new }
before do
allow(view).to receive(:can?).with(:update, current_exhibit).and_return(true)
allow(view).to receive(:can?).with(:create, Spotlight::Exhibit).and_return(true)
Expand Down
2 changes: 1 addition & 1 deletion spec/views/spotlight/exhibits/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spotlight
describe 'spotlight/exhibits/index', type: :view do
let(:exhibits) { Spotlight::Exhibit.none }
let(:ability) { ::Ability.new(user) }
let(:user) { ::User.new }
let(:user) { Spotlight::Engine.user_class.new }

before do
assign(:exhibits, exhibits)
Expand Down
2 changes: 1 addition & 1 deletion spec/views/spotlight/roles/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spotlight
describe 'spotlight/roles/index', type: :view do
let(:user) { stub_model(::User, email: 'jane@example.com') }
let(:user) { stub_model(Spotlight::Engine.user_class, email: 'jane@example.com') }

let(:exhibit) { FactoryGirl.create(:exhibit) }
let(:admin_role) { FactoryGirl.create(:role, role: 'admin', user: user, exhibit: exhibit) }
Expand Down

0 comments on commit fd181f9

Please sign in to comment.