Skip to content

Commit

Permalink
check if every subject has page
Browse files Browse the repository at this point in the history
  • Loading branch information
lislis committed Jun 14, 2019
1 parent e2026e3 commit 63d31c3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 deletions.
3 changes: 3 additions & 0 deletions app/controllers/walks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def edit
end

def update
if (walk_params[:public].present?)
redirect_to walk_path(@walk), notice: t('walk.cannot_publish') unless @walk.publishable?
end
if @walk.update(walk_params)
redirect_to walk_path(@walk), notice: t('walk.edited')
else
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/walks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ def walks_public_status_helper(walk)
walk_status = 'walk'
if walk.stations.present? && walk.stations.count >= 2
walk_status = 'station'
if walk.stations.first.subjects.present?
if walk.every_station_has_subject?
walk_status = 'subject'
if walk.stations.first.subjects.first.pages.present?
if walk.every_subject_has_page?
walk_status = 'page'
if walk.courseline.present?
walk_status = 'courseline'
Expand Down
35 changes: 27 additions & 8 deletions app/models/walk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,38 @@ def editable_by?(current_user)
(current_user) && (user == current_user || current_user.admin?)
end

def publishable?
stations.count >= 2 &&
courseline.present? &&
stations.first.subjects.present? &&
stations.first.subjects.first.pages.present? &&
!public
end

def link_in_frontend
"#{WALK_BASE_URL}/course/#{id}"
end

def next_station_priority
return stations.size
end

def every_station_has_subject?
flag = true
stations.each do |st|
flag = false unless st.subjects.present?
end
flag
end

def every_subject_has_page?
flag = true
stations.each do |st|
flag = false unless st.subjects.present?
st.subjects.each do |sub|
flag = false unless sub.pages.present?
end
end
flag
end

def publishable?
return false unless stations.present? && stations.count >= 2
return false unless every_station_has_subject?
return false unless every_subject_has_page?
return false unless courseline.present?
true
end
end
63 changes: 34 additions & 29 deletions spec/helpers/walks_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
require 'rails_helper'

RSpec.describe WalksHelper, type: :helper do
describe 'walks_public_status_helper' do
it 'returns walk if no stations are created' do
walk_mock = double(stations: false)
expect(helper.walks_public_status_helper(walk_mock)).to eq('walk')
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
expect(helper.walks_public_status_helper(walk)).to eq('walk')
end

it 'returns station if at least 2 stations were created' do
walk_mock = double(stations: double(
count: 2,
first: double(subjects: false)),
courseline: nil)
expect(helper.walks_public_status_helper(walk_mock)).to eq('station')
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
stations = create_list(:station, 2, walk: walk, user: user)

expect(helper.walks_public_status_helper(walk)).to eq('station')
end

it 'returns courseline if courseline was set' do
walk_mock = double(stations: double(
first: double(subjects: double(
first: double(
pages: true))),
count: 2),
courseline: [[12, 12], [12, 12.1]])
expect(helper.walks_public_status_helper(walk_mock)).to eq('courseline')
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
stations = create_list(:station, 2, walk: walk, user: user)
subject1 = create(:subject, station: stations.first, user: user)
page1 = create(:page, subject: subject1, user: user)
subject2 = create(:subject, station: stations.last, user: user)
page2 = create(:page, subject: subject2, user: user)
expect(helper.walks_public_status_helper(walk)).to eq('courseline')
end

it 'returns subject if subject was created' do
walk_mock = double(stations: double(
first: double(
subjects: double(
first: double(
pages: false))),
count: 2),
courseline: nil)
expect(helper.walks_public_status_helper(walk_mock)).to eq('subject')
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
stations = create_list(:station, 2, walk: walk, user: user)
subject1 = create(:subject, station: stations.first, user: user)
page1 = create(:page, subject: subject1, user: user)
subject2 = create(:subject, station: stations.last, user: user)

expect(helper.walks_public_status_helper(walk)).to eq('subject')
end

it 'returns page if page was created' do
walk_mock = double(stations: double(
first: double(subjects: double(
first: double(
pages: true))),
count: 2),
courseline: nil)
expect(helper.walks_public_status_helper(walk_mock)).to eq('page')
user = create(:user, username: 'foo')
walk = create(:walk, courseline: nil, public: false)
stations = create_list(:station, 2, walk: walk, user: user)
subject1 = create(:subject, station: stations.first, user: user)
page1 = create(:page, subject: subject1, user: user)
subject2 = create(:subject, station: stations.last, user: user)
page2 = create(:page, subject: subject2, user: user)

expect(helper.walks_public_status_helper(walk)).to eq('page')
end
end
end
16 changes: 11 additions & 5 deletions spec/models/walk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@
expect(walk.editable_by? current_user).to eq false
end

it 'is not publishable if it is already public' do
walk = create(:walk, public: true)
it 'is not publishable if not all conditions are met' do
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
stations = create_list(:station, 2, walk: walk, user: user)
subjects = create_list(:subject, 2, station: stations.first, user: user)
page = create(:page, subject: subjects.first, user: user)
expect(walk.publishable?).to eq false
end

it 'is publishable if all other conditions are met' do
it 'is publishable if all conditions are met' do
user = create(:user, username: 'foo')
walk = create(:walk, courseline: '[[12, 12], [12.1, 12]]', public: false)
stations = create_list(:station, 2, walk: walk, user: user)
subjects = create_list(:subject, 2, station: stations.first, user: user)
page = create(:page, subject: subjects.first, user: user)
subject1 = create(:subject, station: stations.first, user: user)
page1 = create(:page, subject: subject1, user: user)
subject2 = create(:subject, station: stations.last, user: user)
page2 = create(:page, subject: subject2, user: user)
expect(walk.publishable?).to eq true
end
end

0 comments on commit 63d31c3

Please sign in to comment.