Module: ActiveFedora

Extended by:
ActiveSupport::Autoload
Includes:
Loggable
Defined in:
lib/active_fedora.rb,
lib/active_fedora.rb,
lib/active_fedora/base.rb,
lib/active_fedora/model.rb,
lib/active_fedora/railtie.rb,
lib/active_fedora/version.rb,
lib/active_fedora/property.rb,
lib/active_fedora/datastream.rb,
lib/active_fedora/delegating.rb,
lib/active_fedora/reflection.rb,
lib/active_fedora/associations.rb,
lib/active_fedora/relationship.rb,
lib/active_fedora/solr_service.rb,
lib/active_fedora/content_model.rb,
lib/active_fedora/fedora_object.rb,
lib/active_fedora/semantic_node.rb,
lib/active_fedora/attribute_methods.rb,
lib/active_fedora/nested_attributes.rb,
lib/active_fedora/metadata_datastream.rb,
lib/active_fedora/rels_ext_datastream.rb,
lib/active_fedora/relationships_helper.rb,
lib/active_fedora/associations/association_proxy.rb,
lib/active_fedora/qualified_dublin_core_datastream.rb,
lib/active_fedora/associations/has_many_association.rb,
lib/active_fedora/associations/association_collection.rb,
lib/active_fedora/associations/belongs_to_association.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Associations, AttributeMethods, Delegating, FedoraObject, MetadataDatastreamHelper, Model, NestedAttributes, Reflection, RelationshipsHelper, SemanticNode Classes: ActiveFedoraConfigurationException, AssociationTypeMismatch, Base, ContentModel, Datastream, DatastreamConcurrencyException, MetadataDatastream, NokogiriDatastream, ObjectNotFoundError, PredicateMappingsNotFoundError, Property, QualifiedDublinCoreDatastream, Railtie, Relationship, RelsExtDatastream, ServerError, SolrNotInitialized, SolrService, UnknownAttributeError, UnregisteredPredicateError

Constant Summary collapse

VERSION =
"3.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_envObject

Returns the value of attribute config_env.



51
52
53
# File 'lib/active_fedora.rb', line 51

def config_env
  @config_env
end

.config_optionsObject (readonly)

Returns the value of attribute config_options.



52
53
54
# File 'lib/active_fedora.rb', line 52

def config_options
  @config_options
end

.fedora_configObject

Returns the value of attribute fedora_config.



51
52
53
# File 'lib/active_fedora.rb', line 51

def fedora_config
  @fedora_config
end

.fedora_config_pathObject

Returns the value of attribute fedora_config_path.



51
52
53
# File 'lib/active_fedora.rb', line 51

def fedora_config_path
  @fedora_config_path
end

.solr_configObject

Returns the value of attribute solr_config.



51
52
53
# File 'lib/active_fedora.rb', line 51

def solr_config
  @solr_config
end

.solr_config_pathObject

Returns the value of attribute solr_config_path.



51
52
53
# File 'lib/active_fedora.rb', line 51

def solr_config_path
  @solr_config_path
end

Class Method Details

.check_fedora_path_for_solrObject

Checks the existing fedora_config_path to see if there is a solr.yml there



240
241
242
243
244
245
246
247
# File 'lib/active_fedora.rb', line 240

def self.check_fedora_path_for_solr
  path = fedora_config_path.split('/')[0..-2].join('/') + "/solr.yml"
  if File.file? path
    return path
  else
    return nil
  end
end

.determine_url(config_type, config) ⇒ String

Determines and sets the fedora_config or solr_config

Parameters:

  • config_type (String)

    Either ‘fedora’ or ‘solr’

  • config (Hash)

    The config hash

Returns:

  • (String)

    the solr or fedora url



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_fedora.rb', line 134

def self.determine_url(config_type,config)  
  if config_type == "fedora"
    # support old-style config
    if config[environment].fetch("fedora",nil)
      return config[environment]["fedora"]["url"] if config[environment].fetch("fedora",nil)
    else
      return config[environment]["url"]
    end
  else
    return get_solr_url(config[environment]) if config_type == "solr"
  end
end

.environmentString

Determine what environment we’re running in. Order of preference is:

  1. config_options

  2. Rails.env

  3. ENV

  4. ENV

  5. raises an exception if none of these is set

Examples:

ActiveFedora.init(:environment=>"test")
ActiveFedora.environment => "test"

Returns:

  • (String)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_fedora.rb', line 185

def self.environment
  if config_options.fetch(:environment,nil)
    return config_options[:environment]
  elsif defined?(Rails.env) and !Rails.env.nil?
    return Rails.env.to_s
  elsif defined?(ENV['environment']) and !(ENV['environment'].nil?)
    return ENV['environment']
  elsif defined?(ENV['RAILS_ENV']) and !(ENV['RAILS_ENV'].nil?)
    logger.warn("You're depending on RAILS_ENV for setting your environment. This is deprecated in Rails3. Please use ENV['environment'] for non-rails environment setting: 'rake foo:bar environment=test'")
    ENV['environment'] = ENV['RAILS_ENV']
    return ENV['environment']
  else
    raise "Can't determine what environment to run in!"
  end
end

.fedoraObject



253
254
255
# File 'lib/active_fedora.rb', line 253

def self.fedora
  Fedora::Repository.instance
end

.get_config_path(config_type) ⇒ String

Determine the fedora config file to use. Order of preference is:

  1. Use the config_options if it exists

  2. Look in Rails.root/config/fedora.yml

  3. Look in current working directory/config/fedora.yml

  4. Load the default config that ships with this gem

Parameters:

  • config_type (String)

    Either ‘fedora’ or ‘solr’

Returns:

  • (String)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/active_fedora.rb', line 208

def self.get_config_path(config_type)
  config_type = config_type.to_s
  if (config_path = config_options.fetch("#{config_type}_config_path".to_sym,nil) )
    raise ActiveFedoraConfigurationException unless File.file? config_path
    return config_path
  end
  
  # if solr, attempt to use path where fedora.yml is first
  if config_type == "solr" && (config_path = check_fedora_path_for_solr)
    return config_path
  elsif config_type == "solr" && fedora_config[environment].fetch("solr",nil)
    logger.warn("DEPRECATION WARNING: You appear to be using a deprecated format for your fedora.yml file.  The solr url should be stored in a separate solr.yml file in the same directory as the fedora.yml")
    raise ActiveFedoraConfigurationException
  end

  if defined?(Rails.root)
    config_path = "#{Rails.root}/config/#{config_type}.yml"
    return config_path if File.file? config_path
  end
  
  if File.file? "#{Dir.getwd}/config/#{config_type}.yml"  
    return "#{Dir.getwd}/config/#{config_type}.yml"
  end
  
  # Last choice, check for the default config file
  config_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "#{config_type}.yml"))
  logger.warn "Using the default #{config_type}.yml that comes with active-fedora.  If you want to override this, pass the path to #{config_type}.yml to ActiveFedora - ie. ActiveFedora.init(:#{config_type} => '/path/to/#{config_type}.yml) - or set Rails.root and put #{config_type}.yml into \#{Rails.root}/config."
  return config_path if File.file? config_path
  raise ActiveFedoraConfigurationException "Couldn't load #{config_type} config file!"
end

.get_solr_url(solr_config) ⇒ Object

Given the solr_config that’s been loaded for this environment, determine which solr url to use



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/active_fedora.rb', line 149

def self.get_solr_url(solr_config)
  if @index_full_text == true && solr_config.has_key?('fulltext') && solr_config['fulltext'].has_key?('url')
    return solr_config['fulltext']['url']
  elsif solr_config.has_key?('default') && solr_config['default'].has_key?('url')
    return solr_config['default']['url']
  elsif solr_config.has_key?('url')
    return solr_config['url']
  elsif solr_config.has_key?(:url)
    return solr_config[:url]
  else
    raise URI::InvalidURIError
  end
end

.init(options = {}) ⇒ Object

Initializes ActiveFedora’s connection to Fedora and Solr based on the info in fedora.yml and solr.yml NOTE: this deprecates the use of a solr url in the fedora.yml

If Rails.env is set, it will use that environment. Defaults to “development”. If :environment is not set, order of preference is

  1. Rails.env

  2. ENV

  3. RAILS_ENV

If :fedora_config_path is not set, it will look in

  1. Rails.root/config

  2. current working directory/config

  3. (default) the fedora.yml shipped with gem

If :solr_config_path is not set, it will

  1. look in config_options. If it finds a solr.yml there, it will use it.

  2. If it does not find a solr.yml and the fedora.yml contains a solr url, it will raise an configuration error

  3. If it does not find a solr.yml and the fedora.yml does not contain a solr url, it will look in: Rails.root/config, current working directory/config, then the solr.yml shipped with gem

Parameters:

  • options (Hash) (defaults to: {})

    (optional) a list of options for the configuration of active_fedora

Options Hash (options):

  • :environment (String)

    The environment within which to run

  • :fedora_config_path (String)

    The full path to the fedora.yml config file.

  • :solr_config_path (String)

    The full path to the solr.yml config file.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_fedora.rb', line 83

def self.init( options={} )
  logger.level = Logger::ERROR if logger.respond_to? :level ###MediaShelf StubLogger doesn't have a level= method
  # Make config_options into a Hash if nil is passed in as the value
  options = {} if options.nil?

  # For backwards compatibility, handle cases where config_path (a String) is passed in as the argument rather than a config_options hash
  # In all other cases, set config_path to config_options[:config_path], which is ok if it's nil
  if options.is_a? String
    logger.warn "DEPRECATION WARNING: Calling ActiveFedora.init with a path as an argument is deprecated.  Use ActiveFedora.init(:fedora_config_path=>#{options})"
    @config_options = {:fedora_config_path=>options}
  else
    @config_options = options
  end

  @config_env = environment
  @fedora_config_path = get_config_path(:fedora)
  load_config(:fedora)

  @solr_config_path = get_config_path(:solr)
  load_config(:solr)

  register_fedora_and_solr

  # Retrieve the valid path for the predicate mappings config file
  @predicate_config_path = build_predicate_config_path(File.dirname(fedora_config_path))

end

.load_config(config_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_fedora.rb', line 116

def self.load_config(config_type)
  config_type = config_type.to_s
  config_path = self.send("#{config_type}_config_path".to_sym)

  logger.info("#{config_type.upcase}: loading ActiveFedora.#{config_type}_config from #{File.expand_path(config_path)}")
  config = YAML::load(File.open(config_path))
  raise "The #{@config_env.to_s} environment settings were not found in the #{config_type}.yml config.  If you already have a #{config_type}.yml file defined, make sure it defines settings for the #{@config_env} environment" unless config[@config_env]
  
  config[:url] = determine_url(config_type,config)

  self.instance_variable_set("@#{config_type}_config", config)
  config
end

.load_configsObject



111
112
113
114
# File 'lib/active_fedora.rb', line 111

def self.load_configs
  load_config(:fedora)
  load_config(:solr)
end

.predicate_configObject



257
258
259
# File 'lib/active_fedora.rb', line 257

def self.predicate_config
  @predicate_config_path ||= build_predicate_config_path
end

.register_fedora_and_solrObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/active_fedora.rb', line 163

def self.register_fedora_and_solr
  # Register Solr
  logger.info("FEDORA: initializing ActiveFedora::SolrService with solr_config: #{ActiveFedora.solr_config.inspect}")
  ActiveFedora::SolrService.register(ActiveFedora.solr_config[:url])
  logger.info("FEDORA: initialized Solr with ActiveFedora.solr_config: #{ActiveFedora::SolrService.instance.inspect}")
      
  logger.info("FEDORA: initializing Fedora with fedora_config: #{ActiveFedora.fedora_config.inspect}")
  Fedora::Repository.register(ActiveFedora.fedora_config[:url])
  logger.info("FEDORA: initialized Fedora as: #{Fedora::Repository.instance.inspect}")    
  
end

.solrObject



249
250
251
# File 'lib/active_fedora.rb', line 249

def self.solr
  ActiveFedora::SolrService.instance
end

.versionObject



261
262
263
# File 'lib/active_fedora.rb', line 261

def self.version
  ActiveFedora::VERSION
end