Skip to content

Commit 92f7a08

Browse files
committed
Fuckloads of shit
1 parent c83055b commit 92f7a08

File tree

11 files changed

+131
-53
lines changed

11 files changed

+131
-53
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ gem "grape-activerecord"
88
gem "pg"
99

1010
gem "activeuuid"
11+
gem "ancestry"
1112

1213
gem "require_all"
1314

@@ -16,6 +17,9 @@ group :development do
1617
gem "binding_of_caller"
1718

1819
gem "rubocop"
20+
21+
gem "pry"
22+
gem "gist"
1923
end
2024

2125
group :production do

Gemfile.lock

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ GEM
2525
activeuuid (0.6.1)
2626
activerecord (>= 3.1)
2727
uuidtools
28+
ancestry (2.1.0)
29+
activerecord (>= 3.0.0)
2830
arel (6.0.3)
2931
ast (2.1.0)
3032
astrolabe (1.3.1)
@@ -48,15 +50,16 @@ GEM
4850
coercible (1.0.0)
4951
descendants_tracker (~> 0.0.1)
5052
colorize (0.7.7)
51-
database_cleaner (1.4.1)
53+
database_cleaner (1.5.0)
5254
debug_inspector (0.0.2)
5355
descendants_tracker (0.0.4)
5456
thread_safe (~> 0.3, >= 0.3.1)
5557
diff-lcs (1.2.5)
5658
docile (1.1.5)
5759
equalizer (0.0.11)
5860
erubis (2.7.0)
59-
grape (0.10.1)
61+
gist (4.4.2)
62+
grape (0.13.0)
6063
activesupport
6164
builder
6265
hashie (>= 2.1.0)
@@ -76,6 +79,7 @@ GEM
7679
i18n (0.7.0)
7780
ice_nine (0.11.1)
7881
json (1.8.3)
82+
method_source (0.8.2)
7983
minitest (5.8.0)
8084
multi_json (1.11.2)
8185
multi_xml (0.5.5)
@@ -86,7 +90,11 @@ GEM
8690
rake (>= 0.8.1)
8791
pg (0.18.3)
8892
powerpack (0.1.1)
89-
rack (1.5.5)
93+
pry (0.10.1)
94+
coderay (~> 1.1.0)
95+
method_source (~> 0.8.1)
96+
slop (~> 3.4)
97+
rack (1.6.4)
9098
rack-accept (0.4.5)
9199
rack (>= 0.4)
92100
rack-mount (0.8.3)
@@ -109,7 +117,7 @@ GEM
109117
diff-lcs (>= 1.2.0, < 2.0)
110118
rspec-support (~> 3.3.0)
111119
rspec-support (3.3.0)
112-
rubocop (0.33.0)
120+
rubocop (0.34.0)
113121
astrolabe (~> 1.3)
114122
parser (>= 2.2.2.5, < 3.0)
115123
powerpack (~> 0.1)
@@ -121,6 +129,7 @@ GEM
121129
json (~> 1.8)
122130
simplecov-html (~> 0.10.0)
123131
simplecov-html (0.10.0)
132+
slop (3.6.0)
124133
thread_safe (0.3.5)
125134
tzinfo (1.2.2)
126135
thread_safe (~> 0.1)
@@ -137,14 +146,17 @@ PLATFORMS
137146

138147
DEPENDENCIES
139148
activeuuid
149+
ancestry
140150
better_errors
141151
binding_of_caller
142152
codecov
143153
database_cleaner
154+
gist
144155
grape
145156
grape-activerecord
146157
passenger
147158
pg
159+
pry
148160
rack-test
149161
rake
150162
require_all

api/v1.rb

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,56 @@ class APIv1 < Grape::API
22
format :json
33
rescue_from :all if ENV["RACK_ENV"] == "production"
44

5-
resource :content do
6-
get ":sha1" do
7-
Content.get(name_or_sha1).as_json
5+
resource :users do
6+
get do
7+
User.all.as_json
88
end
99

10-
get "by_user/:user" do |user|
11-
Content.where(user: user).as_json
10+
get ":id" do
11+
User.find(params[:id]).as_json
12+
end
13+
14+
put ":id" do
15+
u = User.find params[:id]
16+
17+
u.username = params[:username]
18+
19+
u.save!
1220
end
21+
end
1322

23+
resource :creations do
1424
get do
15-
Content.all.as_json
25+
Creation.all.as_json
1626
end
1727

18-
put do
19-
c = Content.new(@json)
20-
c.save!
21-
c.as_json
28+
get ":id" do
29+
Creation.find(params[:id]).as_json
30+
end
31+
32+
get ":id/comments" do
33+
c = Creation.find(params[:id])
34+
c.comments.arrange_serializable.map { |h| h.delete_recursive("parent") }
2235
end
2336
end
2437

25-
resource :users do
26-
get do
27-
User.all.as_json
38+
resource :revisions do
39+
get ":sha1" do
40+
Revision.find_by!(sha1: params[:sha1]).as_json
2841
end
2942

30-
get ":id" do |id|
31-
User.find_by(id: id.to_i).as_json
43+
get "by_user/:user" do |user|
44+
Revision.where(user: user).as_json
45+
end
46+
47+
get do
48+
Revision.all.as_json
3249
end
3350

3451
put do
35-
u = User.new(@json)
36-
u.save!
37-
u.as_json
52+
c = Revision.new(@json)
53+
c.save!
54+
c.as_json
3855
end
3956
end
4057
end

config/setup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
ActiveRecord::Base.schema_format = :sql
99

1010
require_all __dir__ + "/../models"
11-
# require_all __dir__ + "/../helpers"
11+
require_all __dir__ + "/../helpers"
1212
require_all __dir__ + "/../api"

db/migrate/20150901155610_init.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ def change
5151

5252
create_table :comments do |t|
5353
t.belongs_to :user
54+
t.belongs_to :creation
5455

55-
t.belongs_to :commentable, polymorphic: true, index: true
56+
t.text :ancestry
5657

5758
t.text :comment
5859
t.integer :rating
@@ -62,5 +63,7 @@ def change
6263
end
6364

6465
add_index :comments, :user_id
66+
add_index :comments, :creation_id
67+
add_index :comments, :ancestry
6568
end
6669
end

db/structure.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ SET default_with_oids = false;
3636
CREATE TABLE comments (
3737
id integer NOT NULL,
3838
user_id integer,
39-
commentable_id integer,
40-
commentable_type character varying,
39+
creation_id integer,
40+
ancestry text,
4141
comment text,
4242
rating integer,
4343
created_at timestamp without time zone,
@@ -235,10 +235,17 @@ ALTER TABLE ONLY users
235235

236236

237237
--
238-
-- Name: index_comments_on_commentable_type_and_commentable_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
238+
-- Name: index_comments_on_ancestry; Type: INDEX; Schema: public; Owner: -; Tablespace:
239+
--
240+
241+
CREATE INDEX index_comments_on_ancestry ON comments USING btree (ancestry);
242+
243+
244+
--
245+
-- Name: index_comments_on_creation_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
239246
--
240247

241-
CREATE INDEX index_comments_on_commentable_type_and_commentable_id ON comments USING btree (commentable_type, commentable_id);
248+
CREATE INDEX index_comments_on_creation_id ON comments USING btree (creation_id);
242249

243250

244251
--

helpers/hash.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Hash
2+
def delete_recursive(to_remove)
3+
delete(to_remove)
4+
each_value do |value|
5+
value.delete_recursive(to_remove) if value.is_a? Hash
6+
value.each { |h| h.delete_recursive(to_remove) if h.is_a? Hash } if value.is_a? Array
7+
end
8+
end
9+
end
10+

models/comment.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
class Comment < ActiveRecord::Base
22
belongs_to :user
3-
belongs_to :commentable, polymorphic: true
3+
belongs_to :creation
44

5-
has_many :comments, as: :commentable
5+
has_ancestry
66

77
validates :user, presence: true
8-
validates :commentable, presence: true
8+
validates :creation, presence: true
99

1010
validates :created_at, presence: true
1111

12-
def as_json(options = {})
13-
super(
14-
{
15-
except: [:user_id, :commentable_id, :commentable_type],
16-
include: {
17-
user: { only: :id }
18-
}
19-
}.merge(options)
20-
).tap do |json|
21-
if self.commentable
22-
json["parent"] = {
23-
id: self.commentable.id,
24-
type: self.commentable.class.name.downcase
25-
}
12+
def serializable_hash(options = nil)
13+
options ||= {}
14+
super({ except: [:user_id, :creation_id, :parent_id, :ancestry] }.merge(options)).tap do |json|
15+
if options[:full_user]
16+
json[:user] = user.as_json
17+
else
18+
json[:user] = { id: user_id }
2619
end
2720

28-
unless self.comments.blank?
29-
json["children"] = self.comments.as_json.each { |c| c.delete("parent") }
21+
if options[:full_creation]
22+
json[:creation] = creation.as_json
23+
else
24+
json["creation"] = { id: creation_id }
25+
end
26+
27+
if parent_id
28+
if options[:full_parent]
29+
json[:parent] = parent.as_json
30+
else
31+
json["parent"] = { id: parent_id }
32+
end
3033
end
3134
end
3235
end
36+
37+
after_initialize :defaults
38+
def defaults
39+
self.created_at ||= DateTime.now if self.has_attribute? :created_at
40+
end
3341
end

models/creation.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@ class Creation < ActiveRecord::Base
22
belongs_to :user
33

44
has_many :revisions
5-
has_many :comments, as: :commentable
5+
has_many :comments
66

77
validates :title, presence: true
88
validates :visibility, inclusion: { in: %w(public unlisted private) }
99
validates :user, presence: true
1010

11-
def as_json(options = {})
11+
def serializable_hash(options = nil)
12+
options ||= {}
1213
super(
1314
{
1415
except: :user_id,
1516
include: {
16-
user: { only: :id }
17+
user: { only: [:id, :username] }
1718
}
1819
}.merge(options)
1920
)
2021
end
22+
23+
after_initialize :defaults
24+
def defaults
25+
self.visibility ||= "public" if self.has_attribute? :visibility
26+
self.tags ||= [] if self.has_attribute? :tags
27+
self.created_at ||= DateTime.now if self.has_attribute? :created_at
28+
end
29+
30+
def comments
31+
Comment.where(creation: self)
32+
end
2133
end

models/revision.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ class Revision < ActiveRecord::Base
33

44
belongs_to :creation
55

6-
validates :sha1, length: { is: 20 }
6+
validates :sha1, length: { is: 40 }
77
validates :filesize, presence: true
8-
validates :type, presence: true
8+
validates :creation, presence: true
99
validates :title, presence: true
10+
validates :type, presence: true
1011
validates :created_at, presence: true
11-
validates :creation, presence: true
1212
end

models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class User < ActiveRecord::Base
1212
validates :created_at, presence: true
1313
validates :last_login, presence: true
1414

15+
def serializable_hash(options = nil)
16+
options ||= {}
17+
super({ except: :mojang_uuid }.merge(options))
18+
end
19+
1520
after_initialize :defaults
1621
def defaults
1722
self.role ||= "user" if self.has_attribute? :role

0 commit comments

Comments
 (0)