Skip to content

Commit

Permalink
Merge pull request spree-contrib#50 from TruemarkDev/spree_4_fix
Browse files Browse the repository at this point in the history
Spree 4 migration
  • Loading branch information
damianlegawiec authored Dec 2, 2020
2 parents 622771a + 662e8aa commit 34c0161
Show file tree
Hide file tree
Showing 40 changed files with 563 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ spec/dummy
pkg/*
Gemfile.lock
gemfiles/*.gemfile.lock

coverage
.byebug_history
4 changes: 3 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--colour
--color
-r spec_helper
-f documentation
51 changes: 35 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
sudo: required
dist: trusty
os: linux
dist: bionic

addons:
apt:
sources:
- google-chrome
packages:
- google-chrome-stable

services:
- mysql
- postgresql

language: ruby

rvm:
- 2.6

env:
- DB=postgres
- DB=mysql
- DB=postgres

gemfile:
- gemfiles/spree_3_1.gemfile
- gemfiles/spree_3_2.gemfile
- gemfiles/spree_3_7.gemfile
- gemfiles/spree_4_0.gemfile
- gemfiles/spree_4_1.gemfile
- gemfiles/spree_master.gemfile

script:
- bundle exec rspec spec
jobs:
allow_failures:
- gemfile: gemfiles/spree_master.gemfile

rvm:
- 2.3.1
- 2.2.7
before_script:
- CHROME_MAIN_VERSION=`google-chrome-stable --version | sed -E 's/(^Google Chrome |\.[0-9]+ )//g'`
- CHROMEDRIVER_VERSION=`curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_MAIN_VERSION"`
- curl "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip" -O
- unzip chromedriver_linux64.zip -d ~/bin
- nvm install 14

addons:
apt:
packages:
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6
script:
- bundle exec rake test_app
- bundle exec rake spec

before_install:
- mysql -u root -e "GRANT ALL ON *.* TO 'travis'@'%';"
13 changes: 7 additions & 6 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
appraise 'spree-3-1' do
gem 'spree', '~> 3.1.0'
appraise 'spree-3-7' do
gem 'spree', '~> 3.7.0'
gem 'rails-controller-testing'
end

appraise 'spree-3-2' do
gem 'spree', '~> 3.2.0'
appraise 'spree-4-0' do
gem 'spree', '~> 4.0.0'
gem 'rails-controller-testing'
end

appraise 'spree-3-3' do
gem 'spree', '~> 3.3.0'
appraise 'spree-4-1' do
gem 'spree', '~> 4.1.0'
gem 'rails-controller-testing'
end

Expand Down
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
source 'http://rubygems.org'

source 'https://rubygems.org'

spree_version = 'master'
gem 'spree', github: 'spree/spree', branch: spree_version
spree_version = '~> 4.0'
gem 'spree', spree_version

gemspec
23 changes: 22 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
require "bundler/gem_tasks"
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
require 'spree/testing_support/extension_rake'

RSpec::Core::RakeTask.new
Rake::Task.define_task(:environment)

task default: :environment do
if Dir['spec/dummy'].empty?
Rake::Task[:test_app].invoke
Dir.chdir('../../')
end
Rake::Task[:spec].invoke
end

desc 'Generates a dummy app for testing'
task test_app: :environment do
ENV['LIB_NAME'] = 'spree_slider'
Rake::Task['extension:test_app'].invoke
end
19 changes: 19 additions & 0 deletions app/helpers/spree/admin/slides_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Spree
module Admin
module SlidesHelper
def get_column_header_by_type(type)
return Spree.t(:name) if type == :image
return Spree.t(:product) if type == :product

return '----'
end

def get_image_link_by_type(slide, type)
return link_to(slide.name, object_url(slide)) if type == :image
return link_to(slide.product.name, object_url(slide)) if type == :product && slide.product

return '----'
end
end
end
end
16 changes: 11 additions & 5 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
Spree::Product.class_eval do
has_one :slide, dependent: :destroy
after_update :destroy_slide_if_deleted
module Spree
module ProductDecorator
def self.prepended(base)
base.has_one :slide
base.after_destroy :destroy_slide_if_deleted
end

def destroy_slide_if_deleted
slide.update_attributes(published: false) if slide && deleted_at
def destroy_slide_if_deleted
slide.update(published: false) if slide && deleted_at
end
end
end

::Spree::Product.prepend(Spree::ProductDecorator)
39 changes: 30 additions & 9 deletions app/models/spree/slide.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
class Spree::Slide < ActiveRecord::Base

has_and_belongs_to_many :slide_locations,
class_name: 'Spree::SlideLocation',
join_table: 'spree_slide_slide_locations'

has_attached_file :image,
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"] }
belongs_to :product, touch: true, optional: true

has_one_attached :image

validates :link_url, presence: true, url: true, unless: -> { product }
validates :name, :image, presence: true, unless: -> { product }
validates :image, 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) }
scope :product_slides, -> { published.where.not(product_id: nil).order('position ASC') }
scope :image_slides, -> { published.where(product_id: nil).order('position ASC') }

belongs_to :product, touch: true, optional: true
STYLES = {
preview: [120, 120],
thumbnail: [240, 240]
}.freeze

def initialize(attrs = nil)
attrs ||= { published: true }
Expand All @@ -29,6 +35,21 @@ def slide_link
end

def slide_image
!image.file? && product.present? && product.images.any? ? product.images.first.attachment : image
!image.attached? && product.present? && product.images.any? ? product.images.first.attachment : image.attachment
end

# Helper for resizing
def preview
image_form(:preview)
end

def thumbnail
image_form(:thumbnail)
end

private

def image_form(form)
slide_image.variant(resize_to_limit: STYLES[form])
end
end
2 changes: 0 additions & 2 deletions app/models/spree/slide_location.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class Spree::SlideLocation < ActiveRecord::Base

has_and_belongs_to_many :slides,
class_name: 'Spree::Slide',
join_table: 'spree_slide_slide_locations'

validates :name, presence: true

end
32 changes: 32 additions & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class UrlValidator < ActiveModel::EachValidator

def validate_each(record, attribute, value)
uri = parse_uri(value)
if !uri
record.errors[attribute] << generic_failure_message
elsif !allowed_protocols.include?(uri.scheme)
record.errors[attribute] << "must begin with #{allowed_protocols_humanized}"
end
end

private

def generic_failure_message
options[:message] || "is an invalid URL"
end

def allowed_protocols_humanized
allowed_protocols.to_sentence(:two_words_connector => ' or ')
end

def allowed_protocols
@allowed_protocols ||= [(options[:allowed_protocols] || ['http', 'https'])].flatten
end

def parse_uri(value)
uri = Addressable::URI.parse(value)
uri.scheme && uri.host && uri
rescue URI::InvalidURIError, Addressable::URI::InvalidURIError, TypeError
end

end
10 changes: 5 additions & 5 deletions app/views/spree/admin/slides/_edit_slider_locations.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
</tbody>
</table>

<%#= link_to_add_fields t('spree_slider_locations.new_location'), "tbody#slider_locations" %>
<%= button_link_to t('spree_slider_locations.new_location'), 'javascript:;', {
icon: 'add',
:'data-target' => 'tbody#slider_locations',
class: 'btn-success spree_add_fields'
icon: 'add',
'data-target': 'tbody#slider_locations',
class: 'btn-success spree_add_fields'
} %>
<br/><br/>
<br/>
<br/>
5 changes: 2 additions & 3 deletions app/views/spree/admin/slides/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@

<div class="col-md-6">
<%= f.field_container :image do %>
<% if f.object.image? %>
<% if f.object.image.attached? %>
<p>
<%= label_tag t(:preview) %>
<br>
<%= image_tag f.object.image, class: 'img-responsive' %>
<%= image_tag main_app.url_for(f.object.thumbnail), class: 'img-responsive' %>
</p>
<% end %>
<% end %>
</div>

</div>
62 changes: 33 additions & 29 deletions app/views/spree/admin/slides/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,37 @@
<%= t('spree_slider.title') %>
<% end %>

<table class="table sortable" id="listing_slides" data-hook data-sortable-link="<%= update_positions_admin_slides_url %>" >
<thead>
<tr data-hook="admin_slides_index_headers">
<th colspan="2"><%= Spree.t(:image) %></th>
<th><%= Spree.t(:name) %></th>
<th><%= Spree.t(:product) %></th>
<th><%= Spree.t(:published) %></th>
<th data-hook="admin_slides_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% @slides.each do |slide|%>
<tr id="<%= spree_dom_id slide %>" data-hook="admin_slides_index_rows">
<td class="no-border">
<span class="handle"></span>
</td>
<td class="align-center"><%= image_tag slide.slide_image, style: 'width: 120px; height: auto;' %></td>
<td class="align-center"><%= link_to slide.name, object_url(slide) %></td>
<td class="align-center"><%= link_to slide.product.name, object_url(slide) unless slide.product_id.blank? %></td>
<td class="align-center"><%= slide.published ? Spree.t(:say_yes) : Spree.t(:say_no) %></td>
<td data-hook="admin_slides_index_row_actions" class="actions">
<%= link_to_edit slide, no_text: true, class: 'edit' %>
&nbsp;
<%= link_to_delete slide, no_text: true %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% product_slides = @slides.where.not(product_id: nil) %>
<% image_slides = @slides.where(product_id: nil) %>

<% [
[I18n.t("spree_slider.image_slides"), image_slides, :image],
[I18n.t("spree_slider.product_slides"), product_slides, :product]
].each do |translation, slides, type|%>
<h3><%= translation %></h3>
<table class="table sortable" id="listing_slides" data-hook data-sortable-link="<%= update_positions_admin_slides_url %>">
<thead>
<tr data-hook="admin_slides_index_headers">
<th><%= Spree.t(:image) %></th>
<th><%= get_column_header_by_type(type) %></th>
<th><%= Spree.t(:published) %></th>
<th data-hook="admin_slides_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% slides.each do |slide| %>
<tr id="<%= spree_dom_id slide %>" data-hook="admin_slides_index_rows">
<td><%= image_tag slide.slide_image ? main_app.url_for(slide.preview) : "noimage/small.png" %></td>
<td><%= get_image_link_by_type(slide, type) %></td>
<td><%= slide.published ? Spree.t(:say_yes) : Spree.t(:say_no) %></td>
<td data-hook="admin_slides_index_row_actions" class="actions text-right">
<%= link_to_edit slide, no_text: true, class: 'edit' %>
&nbsp;
<%= link_to_delete slide, no_text: true %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% end %>
Loading

0 comments on commit 34c0161

Please sign in to comment.