Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
73 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class Project | ||
module Preferences | ||
|
||
# DEFAULT_WORKBENCH_STARTING_PATH = '/hub'.freeze | ||
# DEFAULT_WORKBENCH_SETTINGS = { | ||
# 'workbench_starting_path' => DEFAULT_WORKBENCH_STARTING_PATH | ||
# }.freeze | ||
|
||
BASE_PREFERENCES = YAML.load_file(Rails.root + 'config/preferences/project.yml') | ||
|
||
Project.class_eval do | ||
store :preferences, accessors: BASE_PREFERENCES.symbolize_keys.keys, coder: JSON | ||
before_save :fill_preferences | ||
|
||
# after_initialize :set_default_preferences | ||
|
||
def self.hash_preferences | ||
Project::BASE_PREFERENCES.select{|k, v| Project::BASE_PREFERENCES[k].kind_of?(Hash)}.keys.inject({}){|hsh,k| hsh.merge!(k.to_sym => {})} | ||
end | ||
|
||
def self.array_preferences | ||
Project::BASE_PREFERENCES.select{|k, v| Project::BASE_PREFERENCES[k].kind_of?(Array)}.keys.inject({}){|hsh,k| hsh.merge!(k.to_sym => [])} | ||
end | ||
|
||
def self.key_value_preferences | ||
Project::BASE_PREFERENCES.symbolize_keys.keys.select{|k| Project::BASE_PREFERENCES[k] != {} && Project::BASE_PREFERENCES[k] != []} | ||
end | ||
end | ||
|
||
|
||
|
||
def fill_preferences | ||
if preferences.empty? | ||
reset_preferences | ||
else | ||
BASE_PREFERENCES.keys.each do |k| | ||
preferences[k] = BASE_PREFERENCES[k] if send(k).nil? | ||
end | ||
end | ||
true | ||
end | ||
|
||
def reset_preferences | ||
write_attribute(:preferences, BASE_PREFERENCES) | ||
end | ||
|
||
def layout=(values) | ||
l = layout.nil? ? {} : layout | ||
super(l.merge(values)) | ||
end | ||
|
||
# def set_default_preferences | ||
# write_attribute(:preferences, DEFAULT_WORKBENCH_SETTINGS.merge(preferences ||= {}) ) | ||
# end | ||
|
||
# def clear_preferences | ||
# update_column(:preferences, DEFAULT_WORKBENCH_SETTINGS) | ||
# end | ||
|
||
|
||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# !! YOU MUST RESTART THE SERVER FOR CHANGES HERE TO TAKE AFFECT !! | ||
# | ||
# This is the default project preference set. | ||
# | ||
# * Values of `nil` should be avoided! | ||
# | ||
--- | ||
|
||
workbench_starting_path: '/hub' | ||
is_api_accessible: false | ||
layout: { } # element_set_id: { element_id: position } | ||
model_predicate_sets: | ||
- Otu: [] | ||
- AssertedDistribution: [] | ||
- CollectionObject: [] | ||
- CollectingEvent: [] | ||
default_hub_tab_order: # not ported | ||
- tasks | ||
- data | ||
- favorite |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'rails_helper' | ||
|
||
describe Project, type: :model, group: [:project] do | ||
|
||
let(:project) { Project.new(name: 'I prefer this project') } | ||
|
||
context 'project.yml' do | ||
context 'accessors exist for' do | ||
Project::BASE_PREFERENCES.keys.each do |k| | ||
specify "#{k}" do | ||
expect(project.respond_to?(k)).to be_truthy | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'hashed keys' do | ||
specify '.hash_preferences' do | ||
expect(Project.hash_preferences).to include(layout: {}) | ||
end | ||
|
||
specify '.array_preferences' do | ||
expect(Project.array_preferences).to include(default_hub_tab_order: []) | ||
end | ||
|
||
specify '.key_value_preferences' do | ||
expect(Project.key_value_preferences).to include(:model_predicate_sets) # etc. | ||
end | ||
end | ||
|
||
specify 'filled preferences are not over-written' do | ||
project.is_api_accessible = true | ||
project.save! | ||
expect(project.is_api_accessible).to eq(true) | ||
end | ||
|
||
specify 'unfilled preferences are filled' do | ||
project.save! | ||
expect(project.is_api_accessible).to eq(false) | ||
end | ||
|
||
specify '#reset_preferences' do | ||
project.reset_preferences | ||
expect(project.preferences).to eq(Project::BASE_PREFERENCES) | ||
end | ||
|
||
context 'with one layout set' do | ||
before do | ||
project.layout = { foo: {'firstid' => 1, 'secondid' => 2}} | ||
project.save! | ||
end | ||
|
||
specify 'updating another layout' do | ||
project.layout = {bar: {'second_id' => 1}} | ||
expect(project.layout.keys).to contain_exactly('foo', 'bar') | ||
end | ||
end | ||
|
||
specify '#workbench_starting_path 1' do | ||
project.save! | ||
expect(project.workbench_starting_path).to eq('/hub') | ||
end | ||
|
||
specify '#workbench_starting_path 1' do | ||
project.workbench_starting_path = '/dashboard' | ||
project.save! | ||
expect(project.workbench_starting_path).to eq('/dashboard') | ||
end | ||
|
||
|
||
end |
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