Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added citation and citation_topic linkages, added string utility to t…
…rim and set to nil if empty
- Loading branch information
eef
committed
Nov 5, 2013
1 parent
30228db
commit 2a6d3cd
Showing
18 changed files
with
140 additions
and
47 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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
# Citation is like Roles in that it is also a linking table between a data object & a source. | ||
# (Assertion that the subject was referenced in a source) | ||
class Citation < ActiveRecord::Base | ||
|
||
belongs_to :citation_object, polymorphic: :true | ||
belongs_to :source | ||
belongs_to :source, inverse_of: :citations | ||
|
||
has_many :citation_topics, inverse_of: :citation | ||
|
||
validates_presence_of :citation_object_id, :citation_object_type, :source_id | ||
validates_uniqueness_of :citation_object_id, :citation_object_type, :source_id | ||
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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
class CitationTopic < ActiveRecord::Base | ||
belongs_to :topic | ||
belongs_to :citation | ||
belongs_to :topic, inverse_of: :citation_topics | ||
belongs_to :citation, inverse_of: :citation_topics | ||
|
||
validates_presence_of :topic_id, :citation_id | ||
validates_uniqueness_of :topic_id, :citation_id | ||
|
||
before_save :trim_pages | ||
nil_trim_attributes(:pages) | ||
|
||
|
||
protected | ||
|
||
def trim_pages # pages should have content or be empty | ||
self.pages = self.pages.to_str.strip | ||
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class ControlledVocabularyTerm::Topic < ControlledVocabularyTerm | ||
class ControlledVocabularyTerm::Topic < ControlledVocabularyTerm | ||
has_many :citation_topics, inverse_of: :topic | ||
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
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
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,36 @@ | ||
module ActiverecordUtilities | ||
# this is for shared utilies, like string manipulation methods. | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
# these are the extensions (like has_many) | ||
before_validation :trim_attributes | ||
class_attribute :attributes_to_trim | ||
|
||
end | ||
|
||
module ClassMethods | ||
# any def inside here is a class method | ||
def nil_trim_attributes(*attributes) # this assigns the attributes to be trimmed | ||
self.attributes_to_trim = attributes | ||
end | ||
end | ||
|
||
protected | ||
# any def below this is an instance method | ||
def trim_attributes | ||
if !self.attributes_to_trim.nil? | ||
self.attributes_to_trim.each do |a| | ||
self.send("#{a}=".to_sym, Utilities::Strings.nil_strip(self.send(a))) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
class ActiveRecord::Base | ||
include ActiverecordUtilities | ||
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
|
||
|
||
module TaxonWorks | ||
require 'activerecord_utilities' | ||
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,10 @@ | ||
module Utilities::Strings | ||
|
||
def self.nil_strip(string) # string should have content or be empty | ||
if !string.nil? | ||
string.strip! | ||
string = nil if string == '' | ||
end | ||
string | ||
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,10 @@ | ||
require 'spec_helper' | ||
require 'activerecord_utilities' | ||
|
||
#include dummy class to test the activerecord_utilities | ||
|
||
class TestStringsManipulations | ||
# include ActiverecordUtilities | ||
|
||
|
||
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
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