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

Add time-based slides #34

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 10 additions & 1 deletion app/controllers/spree/admin/slides_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ def location_after_save
end

def slide_params
params.require(:slide).permit(:name, :body, :link_url, :published, :image, :position, :product_id)
params.require(:slide)
.permit(:name,
:body,
:link_url,
:published,
:image,
:position,
:product_id,
:starts_at,
:ends_at)
end
end
end
Expand Down
17 changes: 14 additions & 3 deletions app/models/spree/slide.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Spree::Slide < ActiveRecord::Base

has_and_belongs_to_many :slide_locations,
class_name: 'Spree::SlideLocation',
join_table: 'spree_slide_slide_locations'
Expand All @@ -8,13 +7,21 @@ class Spree::Slide < ActiveRecord::Base
url: '/spree/slides/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/slides/:id/:style/:basename.:extension',
convert_options: { all: '-strip -auto-orient -colorspace sRGB' }
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
validates_attachment :image, content_type: { content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'] }

scope :published, -> { where(published: true).order('position ASC') }
scope :location, -> (location) { joins(:slide_locations).where('spree_slide_locations.name = ?', location) }
scope :location, ->(location) { joins(:slide_locations).where('spree_slide_locations.name = ?', location) }

belongs_to :product, touch: true

def self.in_time
where '(starts_at is NULL AND ends_at is NULL)
OR (starts_at <= ? AND ends_at is NULL)
OR (starts_at is NULL AND ends_at >= ?)
OR (starts_at <= ? AND ends_at >= ?)',
*([Time.now] * 4)
end

def initialize(attrs = nil)
attrs ||= { published: true }
super
Expand All @@ -31,4 +38,8 @@ def slide_link
def slide_image
!image.file? && product.present? && product.images.any? ? product.images.first.attachment : image
end

def in_time?
Time.now.between?(starts_at || 1.second.ago, ends_at || 1.second.from_now)
Copy link

@dportalesr dportalesr Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Favor the use of Time.current instead of Time.now

image
Source

end
end
16 changes: 16 additions & 0 deletions app/views/spree/admin/slides/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
<% end %>
</div>

<div class="col-md-6">
<%= f.field_container :starts_at do %>
<%= f.label :starts_at, 'Starts at' %><br>
<div class="help-block">The slide will not be shown before this date, even if published</div>
<%= f.date_field :starts_at, class: 'fullwidth form-control' %><br>
<% end %>
</div>

<div class="col-md-6">
<%= f.field_container :ends_at do %>
<%= f.label :ends_at, 'Ends at' %><br>
<div class="help-block">The slide will not be shown after this date, even if published</div>
<%= f.date_field :ends_at, class: 'fullwidth form-control' %><br>
<% end %>
</div>

<div class="col-md-6">
<%= render 'spree/admin/slides/edit_slider_locations', f: f %>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddStartsAtAndEndsAtColumnsToSpreeSlides < ActiveRecord::Migration
def change
add_column :spree_slides, :starts_at, :datetime
add_column :spree_slides, :ends_at, :datetime
end
end
68 changes: 68 additions & 0 deletions spec/models/spree/slide_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'spec_helper'

RSpec.describe Spree::Slide do
describe '#in_time?' do
context 'when both starts_at and ends_at are nil' do
subject { Spree::Slide.new starts_at: nil, ends_at: nil }

it { is_expected.to be_in_time }
end

context 'when starts_at is in the past and ends_at is nil' do
subject { Spree::Slide.new starts_at: 2.days.ago, ends_at: nil }

it { is_expected.to be_in_time }
end

context 'when starts_at is in the future and ends_at is nil' do
subject { Spree::Slide.new starts_at: 2.days.from_now, ends_at: nil }

it { is_expected.not_to be_in_time }
end

context 'when starts_at is nil and ends_at is in the future' do
subject { Spree::Slide.new starts_at: nil, ends_at: 2.days.from_now }

it { is_expected.to be_in_time }
end

context 'when starts_at is nil and ends_at is in the past' do
subject { Spree::Slide.new starts_at: nil, ends_at: 2.days.ago }

it { is_expected.not_to be_in_time }
end

context 'when both starts_at and end_at is in the past' do
subject { Spree::Slide.new starts_at: 2.days.ago, ends_at: 1.day.ago }

it { is_expected.not_to be_in_time }
end

context 'when starts_at is in the past and end_at is in the future' do
subject { Spree::Slide.new starts_at: 2.days.ago, ends_at: 2.days.from_now }

it { is_expected.to be_in_time }
end
end

describe '.in_time' do
it 'returns all the slides from the database that are in time' do
good_slides = [
Spree::Slide.create(starts_at: nil, ends_at: nil),
Spree::Slide.create(starts_at: 2.days.ago, ends_at: nil),
Spree::Slide.create(starts_at: nil, ends_at: 2.days.from_now),
Spree::Slide.create(starts_at: 2.days.ago, ends_at: 2.days.from_now)
].map(&:id)

bad_slides = [
Spree::Slide.create(starts_at: 2.days.from_now, ends_at: nil),
Spree::Slide.create(starts_at: nil, ends_at: 2.days.ago),
Spree::Slide.create(starts_at: 2.days.ago, ends_at: 1.day.ago)
].map(&:id)

slides = Spree::Slide.in_time.map(&:id)
expect(slides).to include *good_slides
expect(slides).not_to include *bad_slides
end
end
end