Skip to content

Commit 254a638

Browse files
committed
Added switch to publish anonymously
1 parent bd6225e commit 254a638

11 files changed

+258
-55
lines changed

app/controllers/stories_controller.rb

+51-19
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,17 @@ def create
106106
# PATCH/PUT /stories/1
107107
# PATCH/PUT /stories/1.json
108108
def update
109-
respond_to do |format|
110-
# NOTE: To avoid DEPRECATION WARNING when saving with Globalize attributes
111-
# wait for this pull request to be merged https://github.com/globalize/globalize/pull/629
109+
# NOTE: To avoid DEPRECATION WARNING when saving with Globalize attributes
110+
# wait for this pull request to be merged https://github.com/globalize/globalize/pull/629
112111

113-
# Temporary change I18n.locale for saving the Story using @story_locale
114-
current_locale = I18n.locale
115-
I18n.locale = @story_locale
116-
if @story.update(story_params)
117-
@story.images << images_from_content(@story)
118-
# I18n.locale back to the original
119-
I18n.locale = current_locale
120-
format.html { redirect_to story_path(@story, story_locale: @story_locale), notice: _('Story was successfully updated.') }
121-
format.json { render :show, status: :ok, location: @story }
122-
else
123-
# I18n.locale back to the original
124-
I18n.locale = current_locale
125-
format.html { render :edit }
126-
format.json { render json: @story.errors, status: :unprocessable_entity }
127-
end
112+
# Temporary change I18n.locale for saving the Story using @story_locale
113+
current_locale = I18n.locale
114+
I18n.locale = @story_locale
115+
116+
if params[:story_builder] == 'true'
117+
update_with_story_builder(current_locale)
118+
else
119+
update_with_text_content(current_locale)
128120
end
129121
end
130122

@@ -139,6 +131,9 @@ def destroy
139131
end
140132

141133
def publish
134+
# set params without saving (used only for anonymous)
135+
@story.assign_attributes(publish_params)
136+
142137
if @story.publish!
143138
respond_to do |format|
144139
format.html { redirect_to @story }
@@ -183,9 +178,25 @@ def create_with_story_builder(restore_locale)
183178
end
184179
end
185180

181+
def update_with_story_builder(restore_locale)
182+
if @story.update(story_params)
183+
# I18n.locale back to the original
184+
I18n.locale = restore_locale
185+
respond_to do |format|
186+
format.json { render :show, status: :ok, location: @story }
187+
end
188+
else
189+
# I18n.locale back to the original
190+
I18n.locale = restore_locale
191+
respond_to do |format|
192+
format.json { render json: @story.errors, status: :unprocessable_entity }
193+
end
194+
end
195+
end
196+
186197
# creates a story with a text content
187198
def create_with_text_content(restore_locale)
188-
if @story.save
199+
if @story.save && @story.publish!
189200
@story.images << images_from_content(@story)
190201
I18n.locale = restore_locale
191202
respond_to do |format|
@@ -199,6 +210,23 @@ def create_with_text_content(restore_locale)
199210
end
200211
end
201212

213+
def update_with_text_content(restore_locale)
214+
if @story.update(story_params) && @story.publish!
215+
@story.images << images_from_content(@story)
216+
# I18n.locale back to the original
217+
I18n.locale = restore_locale
218+
respond_to do |format|
219+
format.html { redirect_to story_path(@story, story_locale: @story_locale), notice: _('Story was successfully updated.') }
220+
end
221+
else
222+
# I18n.locale back to the original
223+
I18n.locale = restore_locale
224+
respond_to do |format|
225+
format.html { render :edit }
226+
end
227+
end
228+
end
229+
202230
# This method creates the not-yet-existing tags and replaces
203231
# tag_ids with their ids.
204232
def create_new_tags
@@ -236,4 +264,8 @@ def set_draft_params(pars)
236264
pars[:story][:content_json_draft] = pars[:story][:content_json]
237265
pars[:story][:place_draft] = pars[:story][:place]
238266
end
267+
268+
def publish_params
269+
params.permit(:anonymous)
270+
end
239271
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.publish-anonymously-wrapper {
2+
margin-bottom: 2rem;
3+
text-align: center;
4+
}
5+
6+
.publish-anonymously-wrapper label {
7+
display: block;
8+
font-weight: 500;
9+
}
10+
11+
.bootstrap-switch-primary {
12+
background: #E7472E !important;
13+
}

app/javascript/story-builder/StoryBuilderActions.js

+43-21
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,75 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { publishStory } from './api';
44

5+
import Switch from 'react-bootstrap-switch';
6+
import 'react-bootstrap-switch/dist/css/bootstrap3/react-bootstrap-switch.css';
7+
import './StoryBuilderActions.css';
8+
59
export default class StoryBuilderActions extends Component {
610
static propTypes = {
711
locale: PropTypes.oneOf(['en', 'it', 'hr', 'nl']).isRequired,
812
storyLocale: PropTypes.oneOf(['en', 'it', 'hr', 'nl']).isRequired,
9-
storyId: PropTypes.number.isRequired
13+
storyId: PropTypes.number.isRequired,
14+
anonymous: PropTypes.bool.isRequired
1015
}
1116

1217
constructor(props) {
1318
super(props);
1419
this.state = {
15-
publishing: false
20+
publishing: false,
21+
anonymous: props.anonymous
1622
}
1723
}
1824

1925
componentDidUpdate(prevProps, prevState) {
2026
if (!prevState.publishing && this.state.publishing) {
2127
const { locale, storyLocale, storyId } = this.props;
28+
const { anonymous } = this.state;
2229

23-
publishStory(storyId)
30+
publishStory(storyId, { anonymous })
2431
.then(response => Turbolinks.visit(`/${locale}/stories/${storyId}?story_locale=${storyLocale}`))
2532
.catch(() => this.setState({ publishing: false }))
2633
}
2734
}
2835

2936
render() {
3037
const { locale, storyLocale, storyId } = this.props;
31-
const { publishing } = this.state;
38+
const { publishing, anonymous } = this.state;
3239

3340
return (
34-
<div className="row justify-content-center">
35-
<div className="col-md-6">
36-
<a
37-
href={`/${locale}/stories/${storyId}/preview?story_locale=${storyLocale}`}
38-
className={`btn btn-block btn-outline-cf ${publishing && 'disabled'}`}
39-
role="button"
40-
aria-disabled={publishing}>
41-
Preview
42-
</a>
41+
<div>
42+
<div className="row justify-content-center">
43+
<div className="col-12 publish-anonymously-wrapper">
44+
<label htmlFor="anonymous">Publish anonymously?</label>
45+
<Switch
46+
onChange={(el, state) => this.setState({ anonymous: state })}
47+
value={anonymous}
48+
name="anonymous"
49+
onText="Yes"
50+
offText="No"
51+
disabled={publishing}
52+
/>
53+
</div>
4354
</div>
44-
<div className="col-md-6">
45-
<button
46-
style={{ cursor: 'pointer' }}
47-
onClick={() => this.setState({ publishing: true })}
48-
className="btn btn-block btn-cf"
49-
disabled={publishing}>
50-
Publish
51-
</button>
55+
<div className="row justify-content-center">
56+
<div className="col-6">
57+
<a
58+
href={`/${locale}/stories/${storyId}/preview?story_locale=${storyLocale}`}
59+
className={`btn btn-block btn-outline-cf ${publishing && 'disabled'}`}
60+
role="button"
61+
aria-disabled={publishing}>
62+
Preview
63+
</a>
64+
</div>
65+
<div className="col-6">
66+
<button
67+
style={{ cursor: 'pointer' }}
68+
onClick={() => this.setState({ publishing: true })}
69+
className="btn btn-block btn-cf"
70+
disabled={publishing}>
71+
Publish
72+
</button>
73+
</div>
5274
</div>
5375
</div>
5476
)

app/javascript/story-builder/StoryBuilderWrapper.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class extends Component {
7171
}
7272

7373
render() {
74-
const { locale, storyLocale, availableTags, story: { title_draft, place_draft, content_json_draft, tags } } = this.props;
74+
const { locale, storyLocale, availableTags, story: { title_draft, place_draft, content_json_draft, tags, anonymous } } = this.props;
7575
const { status, storyId } = this.state;
7676

7777
if (this.state.hasError) {
@@ -100,7 +100,12 @@ export default class extends Component {
100100
storyLocale={storyLocale}
101101
/>
102102
{storyId &&
103-
<StoryBuilderActions locale={locale} storyLocale={storyLocale} storyId={storyId} />
103+
<StoryBuilderActions
104+
locale={locale}
105+
storyLocale={storyLocale}
106+
storyId={storyId}
107+
anonymous={anonymous}
108+
/>
104109
}
105110
</div>
106111
)

app/javascript/story-builder/api.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const updateContent = (storyId, story, storyLocale) => {
1616
}
1717

1818
if (storyId) {
19-
return axios.put(`/stories/${storyId}.json?story_locale=${storyLocale}`, { story: normalizedStory }, requestConfig());
19+
return axios.put(`/stories/${storyId}.json?story_builder=true&story_locale=${storyLocale}`, { story: normalizedStory }, requestConfig());
2020
} else {
2121
return axios.post(`/stories.json?story_builder=true&story_locale=${storyLocale}`, { story: normalizedStory }, requestConfig());
2222
}
@@ -42,6 +42,6 @@ export const deleteImage = (commonerId, imageUrl) => {
4242
return axios.delete(`/commoners/${commonerId}/images/${imageId}`, requestConfig());
4343
}
4444

45-
export const publishStory = (storyId) => {
46-
return axios.post(`/stories/${storyId}/publish`, {}, requestConfig());
45+
export const publishStory = (storyId, options = {}) => {
46+
return axios.post(`/stories/${storyId}/publish`, options, requestConfig());
4747
}

app/views/stories/_fields.html.erb

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
label: _('Tags'),
1818
# placeholder: "Managed in JS",
1919
hint: _('Use comma or press Enter to separate tags') %>
20-
<%= f.input :published,
21-
inline_label: _('Published'),
22-
hint: _('Check this to make your story public') %>
2320
<%= f.input :anonymous,
2421
inline_label: _('Publish anonymously') %>
2522

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
json.extract! story, :id, :title_draft, :title, :content_json_draft, :content_json, :place_draft, :place, :commoner_id, :created_at, :updated_at
1+
json.extract! story, :id, :title_draft, :title, :content_json_draft, :content_json, :place_draft, :place, :commoner_id, :created_at, :updated_at, :anonymous
22
json.url story_url(story, format: :json) if story.persisted?
33
json.tags story.tags, :id, :name

app/views/stories/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<div class="story-content">
7979
<%= @story.content&.html_safe %>
8080
<%# TODO: content_json %>
81-
<%= @story.content_json&.map{ |item| item['content'] }.join.html_safe %>
81+
<%= @story.content_json&.map{ |item| item['content'] }&.join&.html_safe %>
8282
</div>
8383
</div>
8484
</div>

config.reek

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ UtilityFunction:
66
DuplicateMethodCall:
77
enabled: false
88

9+
"app/controllers":
10+
TooManyStatements:
11+
enabled: false
12+
913
"db/migrate":
1014
IrresponsibleModule:
1115
enabled: false

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ellipsis.js": "^0.1.1",
1616
"prop-types": "^15.6.1",
1717
"react": "^16.2.0",
18+
"react-bootstrap-switch": "^15.5.3",
1819
"react-dom": "^16.2.0",
1920
"storybuilder-react": "file:./storybuilder-react",
2021
"toastr": "^2.1.2",

0 commit comments

Comments
 (0)