class JournalPaper include Mongoid::Document include Mongoid::Timestamps include Mongoid::Attributes::Dynamic include OrbitModel::Status include MemberHelper include Slug field :paper_title, as: :slug_title, type: String, localize: true field :journal_title, type: String, localize: true field :year, type: String field :language, type: String field :isbn, type: String field :vol_no, type: String field :issue_no, type: String field :form_to_start, type: String field :form_to_end, type: String field :total_pages, type: String field :keywords, type: String field :abstract, type: String field :publication_date, type: DateTime field :url, type: String field :note, type: String paginates_per 10 #relations belongs_to :member_profile belongs_to :journal_paper_type has_and_belongs_to_many :journal_levels has_many :journal_paper_files, autosave: true, dependent: :destroy accepts_nested_attributes_for :journal_paper_files has_and_belongs_to_many :journal_paper_authors, dependent: :destroy accepts_nested_attributes_for :journal_paper_authors has_and_belongs_to_many :journal_paper_author_types after_save :save_journal_paper_files, :save_journal_paper_authors before_validation :add_http validates :paper_title, presence: true def create_link title = [] title << self.member_profile.name if self.member_profile_id.present? title << (!self.journal_paper_authors.blank? ? "#{self.journal_paper_authors.collect{|j| j.name}.join(', ')}" : nil) if !self.publication_date.nil? pd = self.publication_date.strftime("%Y-%m-%d").split('-') title << pd[0] end title << self.paper_title if self.paper_title.present? title << self.journal_title if self.journal_title.present? title << self.vol_no if (self.vol_no.present? && self.vol_no != "0") title << self.issue_no if (self.issue_no.present? && self.issue_no != "0") title << "pp"+self.form_to_start+"-"+self.form_to_end if (self.form_to_start.present? && self.form_to_start != "0") title << ( !self.journal_levels.blank? ? "(#{self.journal_levels.collect{|x| x.title}.join(', ')})" : nil) title.join(', ') end def authors authors = [] authors << self.member_profile.name if self.member_profile_id.present? authors << (!self.journal_paper_authors.blank? ? "#{self.journal_paper_authors.collect{|j| j.name}.join(', ')}" : nil) authors.join(', ') end def self.member_data members = MemberProfile.all member_data = [] members.each do |m| if (m.first_name || m.last_name) member_data << {"memberId" => m.id.to_s, "memberName" => m.name} else member_data << {"memberId" => m.id.to_s, "memberName" => "No Name"} end end member_data.to_json end def save_journal_paper_files self.journal_paper_files.each do |t| if t.should_destroy t.destroy end end end def save_journal_paper_authors self.journal_paper_authors.each do |t| if t.should_destroy t.destroy end end end def values_for_view attribute_values = self.attributes.select{|k,v| v if (k.in?(["year","language","isbn","vol_no","issue_no","form_to_start","form_to_end","total_pages","keywords","abstract","publication_date","url","note"]) && v.present?)} localized_fields = {"paper_title" => self.attributes["paper_title"][I18n.locale], "journal_title" => self.attributes["journal_title"][I18n.locale]} authors = {"authors" => self.authors} files = {} files = self.journal_paper_files.collect do |f| {f.title => url_to_file(f.journal_file.url, f.title)} end values = [attribute_values, localized_fields, authors] + files values.inject(&:merge) end protected def add_http unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//] self.url = 'http://' + self.url end end end