Class: Specify::Service::ViewLoader

Inherits:
Service
  • Object
show all
Defined in:
lib/specify/services/view_loader.rb

Overview

ViewLoaders will upload .views.xml files to the Specify database.

Instance Attribute Summary collapse

Attributes inherited from Service

#agent, #collection, #discipline, #division, #session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#database

Constructor Details

#initialize(host:, database:, collection:, specify_user: nil, level: nil, config:) ⇒ ViewLoader

Returns a new ViewLoader.

level is the level to which the .views.xml file will be uploaded. valid values are :discipline, :collection, { :user_type => String } (String Symbol must be a valid Specify::UserType#name) or { :user => String } (where String must be an existing Specify::Model::User#user name).


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/specify/services/view_loader.rb', line 52

def initialize(host:,
               database:,
               collection:,
               specify_user: nil,
               level: nil,
               config:)
  super(host: host,
        database: database,
        collection: collection,
        specify_user: specify_user,
        config: config)
  @target = nil
  self.target = level
end

Instance Attribute Details

#targetObject

The target to which the ViewLoader uploads .views.xml files; an instance of Specify::Model::Discipline, Specify::Model::Collection, Specify::UserType, or Specify::Model::User


10
11
12
# File 'lib/specify/services/view_loader.rb', line 10

def target
  @target
end

Class Method Details

.from_branch(config:, path: nil, name: nil) ⇒ Object

Creates a new instance from a branch name, if name conforms to the naming convention database_name/collection_name/level, where level is discipline, collection, manager, fullaccess, limitedaccess, or guest), or user/username.

path is the filepath for the view file to be uploaded.

config is the yaml file containing the database configuration.


20
21
22
23
24
25
26
27
# File 'lib/specify/services/view_loader.rb', line 20

def self.from_branch(config:, path: nil, name: nil)
  parser = if name
             BranchParser.new(path, name, config)
           else
             BranchParser.current_branch config
           end
  new parser.to_h.merge(config: config)
end

.user_target(hash) ⇒ Object

Returns the Specify::Model::User instance for hash if hash has the key :user and the value for that key is an existing Specify::Model::User#name.


32
33
34
35
# File 'lib/specify/services/view_loader.rb', line 32

def self.user_target(hash)
  return unless hash.key? :user
  Model::User.first(Name: hash[:user])
end

.user_type_target(hash) ⇒ Object

Returns the Specify::UserType for hash if hash has the key user_type, and the value for that key is a valid Specify::UserType#name.


40
41
42
43
# File 'lib/specify/services/view_loader.rb', line 40

def self.user_type_target(hash)
  return unless hash.key? :user_type
  UserType.new(hash[:user_type])
end

Instance Method Details

#import(file) ⇒ Object

Loads the views.xml file and stores it as a SQL blob in the Specify::Model::ViewSetObject for the #target.


74
75
76
77
78
# File 'lib/specify/services/view_loader.rb', line 74

def import(file)
  view_set = @target.view_set(collection) ||
             create_view_set(file)
  view_set.import(views_file(file))
end

#inspectObject

Creates a string representation of self.


68
69
70
# File 'lib/specify/services/view_loader.rb', line 68

def inspect
  "#{self} database: #{@db.database}, target: #{@target}"
end

#personal?Boolean

Returns true if the view is personal, false otherwise.

Returns:

  • (Boolean)

81
82
83
# File 'lib/specify/services/view_loader.rb', line 81

def personal?
  @target.is_a?(Model::User) ? true : false
end

#view_collectionObject

Returns the Specify::Model::Collection that views.xml files will be uploaded to (all supported upload levels except :disciplin are scoped to a collection. Will be #collection or nil.


105
106
107
# File 'lib/specify/services/view_loader.rb', line 105

def view_collection
  collection unless @target.is_a? Model::Discipline
end

#view_disciplineObject

Returns the Specify::Model::Discipline that views.xml files will be uploaded to.


111
112
113
# File 'lib/specify/services/view_loader.rb', line 111

def view_discipline
  discipline
end

#view_levelObject

Returns 2 if the view is for a collection, 0 otherwise.


116
117
118
# File 'lib/specify/services/view_loader.rb', line 116

def view_level
  @target.is_a?(Model::Collection) ? 2 : 0
end

#view_typeObject

Returns the Specify::Model::Discipline#name, which is the Specify::Model::AppResourceDir#discipline_type.


122
123
124
# File 'lib/specify/services/view_loader.rb', line 122

def view_type
  view_discipline.name
end

#view_userObject

Returns the Specify::Model::User that views.xml files will be uploaded to. Same as #session Specify::Session#user unless the view is for a different user.


129
130
131
# File 'lib/specify/services/view_loader.rb', line 129

def view_user
  @target.is_a?(Model::User) ? @target : @session.user
end

#view_user_typeObject

Returns a String with a valid Specify::UserType#name for #target.


134
135
136
137
138
139
140
141
# File 'lib/specify/services/view_loader.rb', line 134

def view_user_type
  case @target
  when UserType
    @target.name.to_s
  when Model::User
    @target.UserType.downcase
  end
end