Class: Book

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/iatelier/models/book.rb

Direct Known Subclasses

Bundle

Constant Summary collapse

DIMENSIONS =
[]
GROUPINGS =
[]
ROLES =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



28
29
30
# File 'lib/iatelier/models/book.rb', line 28

def namespace
  @namespace
end

Instance Method Details

#contentObject



66
67
68
69
# File 'lib/iatelier/models/book.rb', line 66

def content
	markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
	return markdown.render(content_raw.to_s.force_encoding(Encoding::UTF_8))
end

#content_rawObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/iatelier/models/book.rb', line 52

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

#convert(string) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/iatelier/models/book.rb', line 119

def convert string
	if string.kind_of? String
		require 'json'
		return JSON.parse(string);
	end
	return string;
end

#dimensionsObject



37
38
39
# File 'lib/iatelier/models/book.rb', line 37

def dimensions
	self.class::DIMENSIONS
end

#get_asset(params) ⇒ Object



204
205
206
207
# File 'lib/iatelier/models/book.rb', line 204

def get_asset params
	path = Iatelier.configuration.storage_dir + @namespace.to_s + self.class.name.downcase + '/' + self.id.to_s + '/'
	return File.open(path + params[:asset], 'r')
end

#groupingsObject



40
41
42
# File 'lib/iatelier/models/book.rb', line 40

def groupings
  	self.class::GROUPINGS
end

#individuals(requested_role) ⇒ Object



47
48
49
50
# File 'lib/iatelier/models/book.rb', line 47

def individuals requested_role
	@query = '`peopleables`.`role` = "' + requested_role.to_s + '"'
	self.peoples.includes(:peopleables).where(@query)
end

#kindObject



30
31
32
# File 'lib/iatelier/models/book.rb', line 30

def kind
	self.class.to_s
end

#revise_description(params) ⇒ Object



151
152
153
# File 'lib/iatelier/models/book.rb', line 151

def revise_description params
	self.description.update(value: params[:description])
end

#revise_slug(params) ⇒ Object



144
145
146
# File 'lib/iatelier/models/book.rb', line 144

def revise_slug params
	return self.slug.update(value: params[:slug])
end

#revise_subtitle(params) ⇒ Object



137
138
139
# File 'lib/iatelier/models/book.rb', line 137

def revise_subtitle params
	return self.subtitle.update(value: params[:subtitle])
end

#revise_thumbnail(params) ⇒ Object



157
158
# File 'lib/iatelier/models/book.rb', line 157

def revise_thumbnail params
end

#revise_timestamp(params) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/iatelier/models/book.rb', line 168

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

#revise_title(params) ⇒ Object



130
131
132
# File 'lib/iatelier/models/book.rb', line 130

def revise_title params
	return self.title.update(value: params[:title])
end

#rolesObject



43
44
45
# File 'lib/iatelier/models/book.rb', line 43

def roles
 		self.class::ROLES
end

#setup_description(params) ⇒ Object



148
149
150
# File 'lib/iatelier/models/book.rb', line 148

def setup_description params
	self.build_description({value: params[:description]})
end

#setup_slug(params) ⇒ Object



141
142
143
# File 'lib/iatelier/models/book.rb', line 141

def setup_slug params
	return self.build_slug({value: params[:slug]})
end

#setup_subtitle(params) ⇒ Object



134
135
136
# File 'lib/iatelier/models/book.rb', line 134

def setup_subtitle params
	return self.build_subtitle({value: params[:subtitle]})
end

#setup_thumbnail(params) ⇒ Object



155
156
# File 'lib/iatelier/models/book.rb', line 155

def setup_thumbnail params
end

#setup_timestamp(params) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/iatelier/models/book.rb', line 160

def setup_timestamp params
	puts 'setting up timestamp'
	self.build_timestamp({
		draft: params[:timestamp][:draft],
		publish: params[:timestamp][:publish],
		amend: params[:timestamp][:amend]
	})
end

#setup_title(params) ⇒ Object



127
128
129
# File 'lib/iatelier/models/book.rb', line 127

def setup_title params
	return self.build_title({value: params[:title]})
end

#store_attachment(params) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/iatelier/models/book.rb', line 195

def store_attachment params
	path = Iatelier.configuration.storage_dir + @namespace.to_s + self.class.name.downcase + '/' + self.id.to_s + '/'
	if !params.get(:file).nil?
		Dir.mkdir(path) unless File.exists?(path)
		File.open(path + params[:file][:filename], 'wb') do |file|
			file.write params[:file][:tempfile].read
		end
	end
end

#sync_content(params) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/iatelier/models/book.rb', line 183

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

#sync_individuals(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/iatelier/models/book.rb', line 83

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

#sync_keywords(params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/iatelier/models/book.rb', line 71

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

#uniqObject



33
34
35
# File 'lib/iatelier/models/book.rb', line 33

def uniq
	self.class.to_s + self.id.to_s
end