Skip to content

Commit 8ffabb6

Browse files
committed
added new kind and url to media
1 parent fd5f04f commit 8ffabb6

File tree

11 files changed

+270
-218
lines changed

11 files changed

+270
-218
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

app/enumerations/medium_kind.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MediumKind < EnumerateIt::Base
2+
associate_values video: 1, photo: 2
3+
4+
def self.value_for(string)
5+
string.underscore.upcase!
6+
7+
super(string)
8+
end
9+
end

app/models/medium.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Medium < ActiveRecord::Base
22
belongs_to :micropost
33

4-
mount_base64_uploader :file, ContentUploader
4+
mount_base64_uploader :file, ContentUploader, if: :photo?
55

66
validates :micropost, presence: true
7+
8+
has_enumeration_for :kind, with: MediumKind, create_helpers: true, required: true
79
end

app/services/searches/facebook_page_service.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def find_posts_for(source)
88

99
FacebookProvider.client.get_connections(source.key, KEYS)
1010
.select { |e| e.key? 'object_id' }.each do |post|
11-
condition = breakers! post['object_id'], last, source
11+
condition = breakers post['object_id'], last, source
1212

1313
next if condition == 'next'
1414
break if condition == 'break'
@@ -23,10 +23,10 @@ def find_posts_for(source)
2323

2424
private
2525

26-
FIELDS = %w(id object_id message link created_time full_picture).freeze
26+
FIELDS = %w(id object_id message link created_time full_picture type).freeze
2727
KEYS = "posts?fields=#{FIELDS.join(',')}".freeze
2828

29-
def breakers!(id, last, source)
29+
def breakers(id, last, source)
3030
if reached_limit?(id, last)
3131
'break'
3232
elsif Micropost.exists?(source: source, provider_id: id)
@@ -53,15 +53,19 @@ def create_micropost_using!(post, source)
5353
end
5454

5555
def attach_content_to(post, from:)
56-
Medium.create do |m|
57-
m.micropost = post
58-
m.remote_file_url = from['full_picture']
59-
end
56+
Medium.create micropost: post,
57+
remote_file_url: from['full_picture'],
58+
kind: MediumKind.value_for(from['type']),
59+
url: get_media_video(from['object_id'], from['type'])
6060
end
6161

6262
def attach_troller_to(post, from:)
6363
post.trollers << Troller.new(trollerable: from.club)
6464
end
65+
66+
def get_media_video(object_id, kind)
67+
"https://www.facebook.com/video/embed?video_id=#{object_id}" if kind == 'video'
68+
end
6569
end
6670
end
6771
end

app/uploaders/content_uploader.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
class ContentUploader < BaseUploader; end
1+
class ContentUploader < BaseUploader
2+
def extension_whitelist
3+
%w(jpg jpeg gif png)
4+
end
5+
end

config/locales/medium_kind.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
en:
2+
enumerations:
3+
medium_kind:
4+
video: "Video"
5+
photo: "Photo"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddKindToMedia < ActiveRecord::Migration
2+
def change
3+
add_column :media, :kind, :integer, default: 2
4+
add_index :media, :kind
5+
end
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUrlToMedia < ActiveRecord::Migration
2+
def change
3+
add_column :media, :url, :string
4+
end
5+
end

0 commit comments

Comments
 (0)