Module: ActsAsSourceable::InstanceMethods

Defined in:
lib/acts_as_sourceable/acts_as_sourceable.rb

Instance Method Summary collapse

Instance Method Details

#acts_like_sourceable?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/acts_as_sourceable/acts_as_sourceable.rb', line 97

def acts_like_sourceable?
  true
end

#add_sources(*sources) ⇒ Object Also known as: add_source

Add the given holding_institutions, collections, and items



114
115
116
117
118
119
120
121
122
# File 'lib/acts_as_sourceable/acts_as_sourceable.rb', line 114

def add_sources(*sources)
  raise "Cannot set sources of a #{self.class.name}. They are sourced through #{acts_as_sourceable_options[:through]}" if acts_as_sourceable_options[:through]

  sources = Array(sources).flatten
  sources.each do |source|
    source_scope(source).first_or_create!
  end
  update_sourceable_cache_column(true) if sources.present?
end

#remove_sources(*sources) ⇒ Object Also known as: remove_source

Remove the given holding_institutions, collections, and items



126
127
128
129
130
131
132
133
134
# File 'lib/acts_as_sourceable/acts_as_sourceable.rb', line 126

def remove_sources(*sources)
  raise "Cannot set sources of a #{self.class.name}. They are sourced through #{acts_as_sourceable_options[:through]}" if acts_as_sourceable_options[:through]

  sources = Array(sources).flatten
  sources.each do |source|
    source_scope(source).delete_all
  end
  update_sourceable_cache_column(false) if self.sourceable_registry_entries.empty?
end

#sourced?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/acts_as_sourceable/acts_as_sourceable.rb', line 101

def sourced?
  if acts_as_sourceable_options[:cache_column]
    self[acts_as_sourceable_options[:cache_column]]
  else
    self.class.sourced.uniq(false).exists?(self) # Remove the uniqness check because it allows for better use of the indexes
  end
end

#unsourced?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/acts_as_sourceable/acts_as_sourceable.rb', line 109

def unsourced?
  !sourced?
end