Module: TaliaCore::ActiveSourceParts::Finders

Included in:
TaliaCore::ActiveSource
Defined in:
lib/talia_core/active_source_parts/finders.rb

Overview

All class methods that are used for finding sources.

Instance Method Summary collapse

Instance Method Details

#count(*args) ⇒ Object

Modify the count helper so that it can use the advanced options of the ActiveSource #find routine



50
51
52
53
54
55
56
57
# File 'lib/talia_core/active_source_parts/finders.rb', line 50

def count(*args)
  if((options = args.last).is_a?(Hash))
    options.to_options!
    options.delete(:prefetch_relations) # This is not relevant for counting
    prepare_options!(options)
  end
  super
end

#find(*args) ⇒ Object

Finder also accepts uris as “ids”. There are also some additional options that are accepted:

:find_through

accepts and array with an predicate name and an object value/uri, to search for predicates that match the given predicate/value combination

:type

specifically looks for sources with the given type.

:find_through_inv

like :find_through, but for the “inverse” lookup

:prefetch_relations

if set to “true”, this will pre-load all semantic relations for the sources (experimental, not fully implemented yet)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/talia_core/active_source_parts/finders.rb', line 18

def find(*args)
  prefetching = false
  if(args.last.is_a?(Hash))
    options = args.last
    options.to_options!
    
    # Hack the "default" ordering
    options[:order] = 'id' if(options[:order] == :default)
    
    prefetching =  options.delete(:prefetch_relations)
    if(options.empty?) # If empty we remove the args hash, so that the 1-param uri search works
      args.pop
    else
      prepare_options!(options)
    end
  end
  
  result = if(args.size == 1 && (uri_s = uri_string_for(args[0])))
    src = super(:first, :conditions => { :uri => uri_s })
    raise(ActiveRecord::RecordNotFound, "Not found: #{uri_s}") unless(src)
    src
  else
    super
  end

  prefetch_relations_for(result) if(prefetching)

  result
end

#find_by_partial_local(namespace, local_part, options = {}) ⇒ Object

Find the Sources within the given namespace by a partial local name



77
78
79
80
81
82
83
# File 'lib/talia_core/active_source_parts/finders.rb', line 77

def find_by_partial_local(namespace, local_part, options = {})
  namesp = N::URI[namespace]
  return [] unless(namesp)
  find(:all, { 
    :conditions => [ 'uri LIKE ?', "#{namesp.uri}#{local_part}%" ], 
    :order => "uri ASC"}.merge!(options))
end

#find_by_partial_uri(id, options = {}) ⇒ Object

Find the fist Source that matches the given URI. It’s useful for admin pane, because users visit:

/admin/sources/<source_id>/edit

but that information is not enough, since we store into the database the whole reference as URI:

http://localnode.org/av_media_sources/source_id


91
92
93
# File 'lib/talia_core/active_source_parts/finders.rb', line 91

def find_by_partial_uri(id, options = {})
  find(:all, { :conditions => ["uri LIKE ?", '%' + id + '%'] }.merge!(options))
end

#find_by_uri_token(token, options = {}) ⇒ Object

Find a list of sources which contains the given token inside the local name. This means that the namespace it will be excluded.

Sources in system:
  * http://talia.org/one
  * http://talia.org/two

Source.find_by_uri_token('a') # => [ ]
Source.find_by_uri_token('o') # => [ 'http://talia.org/one', 'http://talia.org/two' ]

NOTE: It internally use a MySQL function, as sql condition, to find the local name of the uri.



70
71
72
73
74
# File 'lib/talia_core/active_source_parts/finders.rb', line 70

def find_by_uri_token(token, options = {})
  find(:all, { 
    :conditions => [ "LOWER(SUBSTRING_INDEX(uri, '/', -1)) LIKE ?", '%' + token.downcase + '%' ], 
    :order => "uri ASC" }.merge!(options))
end