require 'redcarpet' class Book < ActiveRecord::Base self.abstract_class = true DIMENSIONS = [] GROUPINGS = [] ROLES = [] has_one :title, as: :titleable, class_name: 'Dimensions::Title' has_one :slug, as: :slugable, class_name: 'Dimensions::Slug' has_one :subtitle, as: :subtitleable, class_name: 'Dimensions::Subtitle' has_one :description, as: :descriptionable, class_name: 'Dimensions::Description' has_one :thumbnail, as: :thumbnailable, class_name: 'Dimensions::Thumbnail' has_one :timestamp, as: :timestampable, class_name: 'Dimensions::Timestamp' has_many :keywordables, as: :keywordable, dependent: :destroy has_many :keywords, through: :keywordables has_many :peopleables, as: :peopleable, dependent: :destroy has_many :peoples, through: :peopleables has_many :bundleables, as: :bundleable, dependent: :destroy has_many :bundles, through: :bundleables accepts_nested_attributes_for :title, :slug, :subtitle, :description, :thumbnail, :timestamp validates_associated :slug attr_accessor :namespace def kind self.class.to_s end def uniq self.class.to_s + self.id.to_s end def dimensions self.class::DIMENSIONS end def groupings self.class::GROUPINGS end def roles self.class::ROLES end def individuals requested_role @query = '`peopleables`.`role` = "' + requested_role.to_s + '"' self.peoples.includes(:peopleables).where(@query) end def content_raw unless self.id.nil? path = Iatelier.configuration.storage_dir + @namespace.to_s + self.class.name.downcase + '/' + self.id.to_s + '/main.md' puts 'triving to retrive the content from the path = ' + path.to_s if File.exists? path return File.open(path).read else return nil end else return nil end end def content markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) return markdown.render(content_raw.to_s) end # def initialized # super() # puts "content of the dimension: \n" # puts dimensions.inspect # dimensions.each do |dimension| # reference = dimension + able # hasone dimension.to_sym as: reference.to_sym # end # end def create params puts 'these are the stuff were going to add:' + dimensions.to_s dimensions.each do |dimension| method_name = 'setup_' + dimension self.public_send(method_name.to_sym, params) end self.save sync_keywords params sync_individuals params sync_content params end def revise params dimensions.each do |dimension| if self.public_send(dimension.to_sym) method_name = 'revise_' + dimension else method_name = 'setup_' + dimension end self.public_send(method_name.to_sym, params) end dimensions.each do |dimension| self.public_send(dimension.to_sym).save end sync_keywords params sync_individuals params sync_content params end def sync_keywords params @nkeys = [] @keywords = params[:keywords].split ', ' @keywords.each do |keyword| @new_keyword = Keyword.find_or_create_by(:word => keyword) @nkeys << @new_keyword end self.keywords = @nkeys end def sync_individuals params @roles = params[:people] puts "this is what we have:" + @roles.to_s @roles.each do |role, individuals| individuals = individuals.split ', ' @existings = self.individuals(role) puts "here is the new output = " + @existings.to_s @existings.each do |instance| @delete = true individuals.each do |individual| if instance.identifier == individual @delete = false end if @delete instance.delete end end end individuals.each do |individual| if People.find_by(identifier: individual) puts "identifier exists" if self.individuals(role).find_by(identifier: individual) puts "this record already exists = " + self.individuals(role).where(identifier: individual).to_s else self.peopleables.create(role: role, people: People.find_by(identifier: individual)) end else puts "a whole new person entry" person = People.create(identifier: individual) self.peopleables.create(role: role, people: People.find_by(identifier: individual)) end # DELETING THE DELETED ASSOCIATIONS!!! end end end def convert string if string.kind_of? String require 'json' return JSON.parse(string); end return string; end def setup_title params self.build_title({value: params[:title]}) end def revise_title params self.title.update(value: params[:title]) end def setup_subtitle params self.build_subtitle({value: params[:subtitle]}) end def revise_subtitle params self.subtitle.update(value: params[:subtitle]) end def setup_slug params self.build_slug({value: params[:slug]}) end def revise_slug params self.slug.update(value: params[:slug]) end def setup_thumbnail params end def revise_thumbnail params end def setup_timestamp params puts 'setting up timestamp' self.build_timestamp({ draft: params[:timestamp][:draft], publish: params[:timestamp][:publish], amend: params[:timestamp][:amend] }) end def revise_timestamp params puts 'revising timestamp' unless self.timestamp.nil? puts 'we found some > updating' self.timestamp.update({ draft: params[:timestamp][:draft], publish: params[:timestamp][:publish], amend: params[:timestamp][:amend] }) else puts 'initiating new ones!' self.setup_timestamp params end end def sync_content params path = Iatelier.configuration.storage_dir + @namespace.to_s + self.class.name.downcase + '/' + self.id.to_s Dir.mkdir(path) unless File.exists?(path) File.open(path + '/main.md', 'w+') do |file| file.puts params[:content] end puts 'updating loc!!! ' + self.id.to_s self.update(loc: self.id.to_s) self.save puts self.loc.to_s end end