Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the ability to add snippets #37

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions app/jobs/story_pro/create_discussion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def perform(discussion:)

header_landscape_image_url, header_vertical_image_url, card_image_url = extract_image_urls(header_image)

regular_css_elements = StoryPro.get_elements(type: 'elements_regularcss')
fullscreen_css_elements = StoryPro.get_elements(type: 'elements_fullscreencss')

new_discussion = Publisher.new(kind: :discussion, name:, user_id:, category_id:)

# - The `tag` update is not working right now.
Expand All @@ -63,15 +66,14 @@ def perform(discussion:)
header_animation: weighted_sample(fullscreen_header_animation),
text_animation: weighted_sample(fullscreen_header_text_animation),
geometry: weighted_sample(fullscreen_header_geometry),
filter: weighted_sample(fullscreen_header_filter)
filter: weighted_sample(fullscreen_header_filter),
elements_fullscreencss_id: search_elements_by_name(fullscreen_css_elements,
'IMAGEHEADER')

end

area.populate_area 'content' do |element|
stem['content'].each_with_index do |content, index|
element.add 'heading', header: content['header'],
size: "#{index == 0 ? 'h1' : 'h2'}",
transition: rand < 0.3 ? 'opacity-1' : ''

if index == 4
element.add 'colorblock', title: content['header'],
Expand All @@ -81,19 +83,29 @@ def perform(discussion:)
text_animation: weighted_sample(fullscreen_header_text_animation),
geometry: weighted_sample(fullscreen_header_geometry),
filter: weighted_sample(fullscreen_header_filter)
else
element.add 'heading', header: content['header'],
size: "#{index == 0 ? 'h1' : 'h2'}",
transition: rand < 0.3 ? 'opacity-1' : '',
elements_regularcss_id: search_elements_by_name(regular_css_elements,
'HEADING', 50)

end


content['paragraphs'].each_with_index do |paragraph, paragraph_index|
element.add 'richtext', rich: "<p>#{paragraph}</p>",
dropcap: !dropcap_shown ? 'show' : 'hide',
dropcap_background_color: !dropcap_shown ? found_color['name'] : '',
transition: weighted_sample(richtext_transitions)
transition: weighted_sample(richtext_transitions),
elements_regularcss_id: search_elements_by_name(regular_css_elements,
'RICHTEXT')
dropcap_shown = true

if paragraph_index == 2 and content['paragraphs'].count > 3 and (index == 2 || index == 3 ||index == 4 || index == 7)
element.add 'spacer', size: 'large'
element.add 'divider'
element.add 'divider', elements_regularcss_id: search_elements_by_name(regular_css_elements,
'DIVIDER')
end
end

Expand All @@ -107,9 +119,6 @@ def perform(discussion:)
geometry: weighted_sample(fullscreen_header_geometry),
filter: weighted_sample(fullscreen_header_filter)
end

# element.add 'spacer', size: 'large' unless index == stem['content'].length - 1
# element.add 'divider' unless index == stem['content'].length - 1
end
end

Expand Down Expand Up @@ -172,6 +181,29 @@ def delete_and_log_error(discussion, error)
end
end

def search_elements_by_name(elements, search_term, percentage = nil)
element_hashes = elements.select do |e|
fields = e['element']['fields']
name = fields['name']
name && name.include?(search_term)
end

if element_hashes.empty?
return ''
end

chosen_element_hash = element_hashes.sample
id = chosen_element_hash['element']['id']

if percentage
# rand(100) returns a number between 0 and 99. Adding 1 to get a number between 1 and 100.
rand_val = rand(100) + 1
return rand_val <= percentage ? id : ''
else
return id
end
end

def weighted_sample(transitions)
weighted_transitions = transitions.flat_map { |transition, weight| [transition] * weight }
weighted_transitions.sample
Expand Down Expand Up @@ -280,8 +312,5 @@ def fullscreen_header_overlay_background
['fixed', 1]
]
end



end
end
7 changes: 5 additions & 2 deletions app/lib/story_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,11 @@ def self.delete_category(id)
send_request(:delete, "categories/#{id}")
end

def self.get_elements
send_get_request('elements')
def self.get_elements(type: nil)
query_params = {}
query_params['type'] = type if type

send_get_request('elements', query_params)
end

def self.add_element(element:, parent_id:, parent_component:, area:)
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/images/upload_imagination_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

described_class.perform_now

expect(Uploadcare::UploadApi).to have_received(:upload_file).with('http://example.com/image.jpg')
expect(Uploadcare::UploadApi).to have_received(:upload_file).with('http://example.com/image.jpg', {:store=>true})
expect(successful_unuploaded_imagination).to have_received(:update!).with(uploadcare: uploadcare_response, uploaded: true)
end
end
Expand Down