require 'active_support/inflector' Dir[File.dirname(__FILE__) + '/*.rb'].each {|file| require file } Dir[File.dirname(__FILE__) + '/dimensions/*.rb'].each {|file| require file } require 'redcarpet' class Book < ActiveRecord::Base self.abstract_class = true @@dimensions = [] @@roles = [] @@content = '' 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 accepts_nested_attributes_for :title, :slug, :subtitle, :description, :thumbnail, :timestamp validates_associated :slug def dimensions @@dimensions end def roles @@roles end def individuals requested_role @query = '`peopleables`.`role` = "' + requested_role.to_s + '"' self.peoples.includes(:peopleables).where(@query) end def content puts "we are here" unless self.id.nil? @storage_location = './storage/books/' + self.class.name.pluralize(2) + '/' + self.id.to_s + '/main.md' puts "now wer are going here :: " + @storage_location if File.exists? @storage_location puts "contnet is equal " + @@content @@content = File.open(@storage_location).read else @@content = nil end else @@content = nil end end def content_to_html markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) markdown.render(self.content) end def write_content text # if File.open(@storage_location).write # self.loc = self.id # self.save 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 @@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 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 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_attributes({value: params[:title]}) end def setup_slug params self.build_slug({value: params[:slug]}) end def revise_slug params self.slug.update_attributes({value: params[:slug]}) end end