This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
153 lines (119 loc) · 2.92 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
require 'sinatra'
require 'slim'
require 'dm-core'
require 'dm-migrations'
require 'unicode'
require 'rake'
configure do
set :public_folder, Proc.new { File.join(root, "bootstrap") }
end
class Object
def present?
!blank?
end
def blank?
respond_to?(:empty?) ? empty? : !self
end
end
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/my.db")
DataMapper::Property::String.length(255)
class Catalog
include DataMapper::Resource
belongs_to :brand
property :id, Serial
property :page_title, String
property :title, String
property :meta_description, Text
property :meta_keywords, Text
property :text, Text
end
class Brand
include DataMapper::Resource
property :title, String
property :page_title, String
property :meta_description, Text
property :meta_keywords, Text
property :translit, String
property :slug, String, key: true
property :country, String
property :year, Integer
property :founder, String
property :ceo, String
property :site, String
property :desc, Text
has n, :categorizations
has n, :categories, through: :categorizations
has 1, :catalog
end
class Category
include DataMapper::Resource
property :slug, String, key: true
property :title, String
has n, :categorizations
has n, :brands, through: :categorizations
end
class Categorization
include DataMapper::Resource
property :id, Serial
belongs_to :category
belongs_to :brand
end
DataMapper.finalize
DataMapper.auto_upgrade!
Categories = ['obuv'].freeze
helpers do
def link_to_category(category)
%{<a href="/#{category.slug}">#{category.title}</a>}
end
end
not_found do
slim :'404', layout: false
end
get '/' do
slim :home, locals: {
brands: Brand.all,
title: "Каталог Брендов"
}
end
get '/updatedb' do
unless Rake::Task.task_defined?('import')
load File.expand_path(File.dirname(__FILE__) + '/Rakefile')
end
Rake::Task['clean'].invoke
Rake::Task['import'].invoke
Rake::Task['clean'].reenable
Rake::Task['import'].reenable
slim "<pre>ok</pre>", layout: false
end
get '/catalog-:s' do |slug|
if (brand = Brand.get(slug)) && (catalog = brand.catalog)
slim '== text', locals: {
text: catalog.text,
title: catalog.title,
meta_description: catalog.meta_description,
meta_keywords: catalog.meta_keywords,
page_title: catalog.page_title
}
else
404
end
end
get '/:s' do |slug|
if brand = Brand.get(slug)
slim :brand, locals: {
brand: brand,
title: "#{brand.title}<small> / #{brand.translit}</small>",
page_title: brand.page_title,
meta_description: brand.meta_description,
meta_keywords: brand.meta_keywords
}
elsif category = Category.get(slug)
slim :category, locals: {
category: category,
title: Unicode::capitalize(category.title),
}
else
404
end
end