spree_zaez_variants_labels 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +12 -0
  5. data/Gemfile +7 -0
  6. data/Guardfile +82 -0
  7. data/LICENSE +26 -0
  8. data/README.md +49 -0
  9. data/Rakefile +21 -0
  10. data/app/assets/javascripts/spree/backend/app/labels/label_controller.js.coffee +36 -0
  11. data/app/assets/javascripts/spree/backend/spectrum.js +2317 -0
  12. data/app/assets/javascripts/spree/backend/spree_zaez_variants_labels.js +2 -0
  13. data/app/assets/javascripts/spree/frontend/spree_zaez_variants_labels.js +2 -0
  14. data/app/assets/stylesheets/spree/backend/spectrum.css +507 -0
  15. data/app/assets/stylesheets/spree/backend/spectrum_overrides.css +29 -0
  16. data/app/assets/stylesheets/spree/backend/spree_zaez_variants_labels.css +4 -0
  17. data/app/assets/stylesheets/spree/frontend/spree_zaez_variants_labels.css +4 -0
  18. data/app/controllers/spree/admin/labels_controller.rb +23 -0
  19. data/app/models/spree/label.rb +8 -0
  20. data/app/models/spree/variant_decorator.rb +5 -0
  21. data/app/overrides/add_label_management_to_variants.rb +4 -0
  22. data/app/overrides/add_labels_to_menu.rb +4 -0
  23. data/app/views/spree/admin/labels/_form.html.erb +26 -0
  24. data/app/views/spree/admin/labels/edit.html.erb +17 -0
  25. data/app/views/spree/admin/labels/index.html.erb +59 -0
  26. data/app/views/spree/admin/labels/new.html.erb +14 -0
  27. data/app/views/spree/admin/variants/_labels.html.erb +12 -0
  28. data/bin/rails +7 -0
  29. data/config/locales/en.yml +5 -0
  30. data/config/routes.rb +10 -0
  31. data/db/migrate/20150706192427_create_spree_labels.rb +8 -0
  32. data/db/migrate/20150707130840_create_spree_labels_variants.rb +8 -0
  33. data/lib/generators/spree_zaez_variants_labels/install/install_generator.rb +31 -0
  34. data/lib/spree_zaez_variants_labels.rb +2 -0
  35. data/lib/spree_zaez_variants_labels/engine.rb +20 -0
  36. data/lib/spree_zaez_variants_labels/factories.rb +13 -0
  37. data/spec/features/spree/admin/labels_spec.rb +174 -0
  38. data/spec/features/spree/admin/variants_spec.rb +23 -0
  39. data/spec/models/spree/label_spec.rb +16 -0
  40. data/spec/models/spree/variant_decorator_spec.rb +9 -0
  41. data/spec/spec_helper.rb +91 -0
  42. data/spec/support/capybara_helpers.rb +13 -0
  43. data/spree_zaez_variants_labels.gemspec +38 -0
  44. metadata +345 -0
@@ -0,0 +1,29 @@
1
+ .sp-container {
2
+ background-color: white;
3
+ border: solid 1px #e0e0e0;
4
+ }
5
+
6
+ .sp-replacer {
7
+ background-color: white;
8
+ border-color: #e0e0e0;
9
+ width: 100%;
10
+ line-height: 1.42857143;
11
+ padding: 8px 16px;
12
+ }
13
+
14
+ .sp-replacer:hover, .sp-replacer.sp-active {
15
+ border-color: #e0e0e0;
16
+ }
17
+
18
+ .sp-preview {
19
+ width: 80%;
20
+ }
21
+
22
+ .sp-dd {
23
+ width: 10%;
24
+ float: right;
25
+ }
26
+
27
+ .sp-palette-button-container{
28
+ margin-top: 8px;
29
+ }
@@ -0,0 +1,4 @@
1
+ /*
2
+ *= require ./spectrum
3
+ *= require ./spectrum_overrides
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/frontend/all.css'
4
+ */
@@ -0,0 +1,23 @@
1
+ module Spree
2
+ module Admin
3
+ class LabelsController < ResourceController
4
+ def index
5
+ session[:return_to] = request.url
6
+ respond_with @collection
7
+ end
8
+
9
+ def collection
10
+ return @collection if @collection.present?
11
+ params[:q] ||= {}
12
+ @collection = super
13
+ # @search needs to be defined as this is passed to search_form_for
14
+ Ransack.options[:ignore_unknown_conditions] = false
15
+ @search = @collection.ransack(params[:q])
16
+ @collection = @search.result.
17
+ page(params[:page]).
18
+ per(params[:per_page] || Spree::Config[:admin_products_per_page])
19
+ @collection
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ class Spree::Label < Spree::Base
2
+
3
+ has_and_belongs_to_many :variants, class_name: 'Spree::Variant'
4
+
5
+ validates_presence_of :name, :color
6
+ validates_uniqueness_of :name
7
+
8
+ end
@@ -0,0 +1,5 @@
1
+ Spree::Variant.class_eval do
2
+
3
+ has_and_belongs_to_many :labels, class_name: 'Spree::Label'
4
+
5
+ end
@@ -0,0 +1,4 @@
1
+ Deface::Override.new virtual_path: 'spree/admin/variants/_form',
2
+ name: 'add_label_management_to_variants',
3
+ insert_top: '[data-hook="admin_variant_form_fields"]',
4
+ partial: 'spree/admin/variants/labels'
@@ -0,0 +1,4 @@
1
+ Deface::Override.new virtual_path: 'spree/admin/shared/sub_menu/_product',
2
+ name: 'add_labels_to_menu',
3
+ insert_bottom: '[data-hook="admin_product_sub_tabs"]',
4
+ text: '<%= tab :labels %>'
@@ -0,0 +1,26 @@
1
+ <div class="row" data-hook="new_label_attrs">
2
+ <div class="col-md-6">
3
+ <%= f.field_container :name, :class => ['form-group'] do %>
4
+ <%= f.label :name, Spree.t(:name) %> <span class="required">*</span>
5
+ <%= f.text_field :name, class: 'form-control title' %>
6
+ <% f.error_message_on :name %>
7
+ <% end %>
8
+ </div>
9
+ <div class="col-md-6">
10
+ <%= f.field_container :color, :class => ['form-group'] do %>
11
+ <%= f.label :color, Spree.t(:color) %>
12
+ <div class="row">
13
+ <div class="col-md-3">
14
+ <input type="text" id="colorpicker" class="form-control" />
15
+ </div>
16
+ <div class="col-md-9">
17
+ <%= f.text_field :color, class: 'form-control color-text' %>
18
+ </div>
19
+ </div>
20
+ <%= f.error_message_on :color %>
21
+ <% end %>
22
+ </div>
23
+ </div>
24
+ <script type="text/javascript">
25
+ new window.LabelController('<%= @label.color %>');
26
+ </script>
@@ -0,0 +1,17 @@
1
+ <% content_for :page_title do %>
2
+ <%= link_to Spree.t(:labels), admin_labels_path %> /
3
+ <%= @label.name %>
4
+ <% end %>
5
+
6
+ <% content_for :page_actions do %>
7
+ <%= button_link_to Spree.t(:new_label), new_object_url, {class: 'btn-success', icon: 'add', id: 'admin_new_label'} %>
8
+ <% end %>
9
+
10
+ <%= render partial: 'spree/admin/shared/error_messages', locals: {target: @label } %>
11
+
12
+ <%= form_for [:admin, @label], method: :put do |f| %>
13
+ <fieldset>
14
+ <%= render partial: 'form', locals: {f: f } %>
15
+ <%= render partial: 'spree/admin/shared/edit_resource_links' %>
16
+ </fieldset>
17
+ <% end %>
@@ -0,0 +1,59 @@
1
+ <% content_for :page_title do %>
2
+ <%= plural_resource_name(Spree::Label) %>
3
+ <% end %>
4
+
5
+ <% content_for :page_actions do %>
6
+ <%= button_link_to Spree.t(:new_label), new_object_url, {class: 'btn-success', icon: 'add', id: 'admin_new_label'} %>
7
+ <% end %>
8
+
9
+ <% content_for :table_filter do %>
10
+ <div class="" data-hook="admin_labels_sidebar">
11
+ <%= search_form_for [:admin, @search] do |f| %>
12
+ <div class="row" data-hook="admin_labels_index_search">
13
+ <div class="col-md-6">
14
+ <div class="form-group">
15
+ <%= f.label :name_cont, Spree.t(:name) %>
16
+ <%= f.text_field :name_cont, size: 15, class: 'form-control js-quick-search-target' %>
17
+ </div>
18
+ </div>
19
+ </div>
20
+ <div class="form-actions" data-hook="admin_labels_index_search_buttons">
21
+ <%= button Spree.t(:search), 'search' %>
22
+ </div>
23
+ <% end %>
24
+ </div>
25
+ <% end %>
26
+
27
+
28
+ <%= render partial: 'spree/admin/shared/index_table_options', locals: {collection: @collection} %>
29
+
30
+ <% if @collection.any? %>
31
+ <table class="table" id="listing_labels">
32
+ <thead>
33
+ <tr data-hook="admin_labels_headers">
34
+ <th><%= Spree.t(:name) %></th>
35
+ <th><%= Spree.t(:color) %></th>
36
+ <th data-hook="admin_labels_index_header_actions" class="actions"></th>
37
+ </tr>
38
+ </thead>
39
+ <tbody>
40
+ <% @collection.each do |label| %>
41
+ <tr data-hook="admin_labels_index_rows" class="<%= cycle('odd', 'even') %>">
42
+ <td><%= label.name %></td>
43
+ <td><%= label.color %></td>
44
+ <td class="actions actions-2 text-right" data-hook="admin_labels_index_row_actions">
45
+ <%= link_to_edit label, no_text: true, class: 'edit' %>
46
+ <%= link_to_delete label, no_text: true %>
47
+ </td>
48
+ </tr>
49
+ <% end %>
50
+ </tbody>
51
+ </table>
52
+ <% else %>
53
+ <div class="alert alert-info no-objects-found">
54
+ <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Label)) %>,
55
+ <%= link_to Spree.t(:add_one), new_object_url %>!
56
+ </div>
57
+ <% end %>
58
+
59
+ <%= render partial: 'spree/admin/shared/index_table_options', locals: {collection: @collection} %>
@@ -0,0 +1,14 @@
1
+ <%= render partial: 'spree/admin/shared/error_messages', locals: {target: @label} %>
2
+
3
+ <% content_for :page_title do %>
4
+ <%= Spree.t(:new_label) %>
5
+ <% end %>
6
+
7
+ <%= form_for [:admin, @label] do |f| %>
8
+ <fieldset data-hook="new_label">
9
+
10
+ <%= render partial: 'form', locals: {f: f} %>
11
+ <%= render partial: 'spree/admin/shared/new_resource_links' %>
12
+
13
+ </fieldset>
14
+ <% end %>
@@ -0,0 +1,12 @@
1
+ <div data-hook="labels">
2
+ <label><%= Spree.t(:label) %></label>
3
+ <ul>
4
+ <% Spree::Label.all.each do |label| %>
5
+ <li>
6
+ <label for="label-<%= label.name %>">
7
+ <%= check_box_tag 'variant[label_ids][]', label.id, @variant.labels.include?(label), {id: "label-#{label.name}"} %> <%= label.name %>
8
+ </label>
9
+ </li>
10
+ <% end %>
11
+ </ul>
12
+ </div>
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_zaez_variants_labels/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,10 @@
1
+ Spree::Core::Engine.routes.draw do
2
+
3
+ namespace :admin do
4
+
5
+ resources :labels
6
+
7
+ end
8
+
9
+
10
+ end
@@ -0,0 +1,8 @@
1
+ class CreateSpreeLabels < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_labels do |t|
4
+ t.string :name
5
+ t.string :color
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class CreateSpreeLabelsVariants < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_labels_variants, :id => false do |t|
4
+ t.references :label
5
+ t.references :variant
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ module SpreeZaezVariantsLabels
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ class_option :auto_run_migrations, :type => :boolean, :default => false
6
+
7
+ def add_javascripts
8
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/spree_zaez_variants_labels\n"
9
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/spree_zaez_variants_labels\n"
10
+ end
11
+
12
+ def add_stylesheets
13
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/spree_zaez_variants_labels\n", :before => /\*\//, :verbose => true
14
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/spree_zaez_variants_labels\n", :before => /\*\//, :verbose => true
15
+ end
16
+
17
+ def add_migrations
18
+ run 'bundle exec rake railties:install:migrations FROM=spree_zaez_variants_labels'
19
+ end
20
+
21
+ def run_migrations
22
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
23
+ if run_migrations
24
+ run 'bundle exec rake db:migrate'
25
+ else
26
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ require 'spree_core'
2
+ require 'spree_zaez_variants_labels/engine'
@@ -0,0 +1,20 @@
1
+ module SpreeZaezVariantsLabels
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_zaez_variants_labels'
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+ def self.activate
13
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
14
+ Rails.configuration.cache_classes ? require(c) : load(c)
15
+ end
16
+ end
17
+
18
+ config.to_prepare &method(:activate).to_proc
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ FactoryGirl.define do
2
+
3
+
4
+ factory :label, class: Spree::Label do
5
+
6
+ colors = ['#5bb85b', '#777777', '#337ab7', '#d9534f']
7
+
8
+ sequence(:name) {|n| "Label-#{n}"}
9
+ color colors[Random.rand(0..3)]
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Label CRUD', {type: :feature, js: true} do
4
+
5
+ let!(:user) {create(:admin_user)}
6
+ let!(:label) {create(:label)}
7
+
8
+ before do
9
+ sign_in_admin! user
10
+ end
11
+
12
+ describe 'index' do
13
+
14
+ before { 10.times{create(:label)} }
15
+
16
+ before :each do
17
+ visit spree.admin_labels_path
18
+ end
19
+
20
+ context 'listing' do
21
+
22
+ it 'must list all labels' do
23
+ expect(page).to have_selector('tr[data-hook="admin_labels_index_rows"]', count: 10)
24
+ end
25
+
26
+ end
27
+
28
+ context 'filtering' do
29
+
30
+ before do
31
+ create(:label, name: 'Filtering Test')
32
+ end
33
+
34
+ it 'must filter using a quick search' do
35
+ within find('#quick-search') do
36
+ fill_in 'quick_search', with: 'Filtering Test'
37
+ find('.js-quick-search').native.send_keys(:Enter)
38
+ end
39
+ expect(page).to have_selector('tr[data-hook="admin_labels_index_rows"]', count: 1)
40
+ end
41
+
42
+ it 'must be filtered by name' do
43
+ find('.js-show-index-filters').click
44
+ expect(page).to have_selector('#table-filter')
45
+ fill_in 'q[name_cont]', with: 'Filtering Test'
46
+ find('button[type="submit"]').click
47
+ expect(page).to have_selector('tr[data-hook="admin_labels_index_rows"]', count: 1)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ describe 'create' do
55
+
56
+ before :each do
57
+ visit spree.new_admin_label_path
58
+ end
59
+
60
+ context 'without errors' do
61
+
62
+ it 'must create the label and redirect to the listing with a success message' do
63
+ within find('#new_label') do
64
+ fill_in 'label[name]', with: 'Label test'
65
+ fill_in 'label[color]', with: '#CC0000'
66
+ find('button[type="submit"]').click
67
+ end
68
+ expect(page).to have_selector('tr[data-hook="admin_labels_index_rows"]')
69
+ expect(page).to have_content 'successfully created!'
70
+ end
71
+
72
+ end
73
+
74
+ context 'with errors' do
75
+
76
+ before {create(:label, name: 'Label Test')}
77
+ before :each do
78
+ visit spree.new_admin_label_path
79
+ end
80
+
81
+ it 'must not be created if name is already taken' do
82
+ within find('#new_label') do
83
+ fill_in 'label[name]', with: 'Label Test'
84
+ fill_in 'label[color]', with: '#CC0000'
85
+ find('button[type="submit"]').click
86
+ end
87
+ expect(page).to have_content 'Name has already been taken'
88
+ end
89
+
90
+ it 'must not be created with invalid params' do
91
+ within find('#new_label') do
92
+ fill_in 'label[name]', with: 'Label Test2'
93
+ find('button[type="submit"]').click
94
+ end
95
+ expect(page).to have_content "Color can't be blank"
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ describe 'edit' do
103
+
104
+ let!(:label){create(:label, name: 'Label editing test')}
105
+ let!(:label_2){create(:label, name: 'Test')}
106
+
107
+ before :each do
108
+ visit spree.edit_admin_label_path(label.id)
109
+ end
110
+
111
+ context 'without errors' do
112
+
113
+ it 'must update the label attributes and redirect to listing with a success message' do
114
+ within find('.edit_label') do
115
+ fill_in 'label[name]', with: 'Name updated'
116
+ find('button[type="submit"]').click
117
+ end
118
+ expect(page).to have_selector('tr[data-hook="admin_labels_index_rows"]')
119
+ expect(page).to have_content 'successfully updated!'
120
+ end
121
+
122
+ end
123
+
124
+ context 'with errors' do
125
+
126
+ it 'must not be updated if the name is already taken' do
127
+ within find('.edit_label') do
128
+ fill_in 'label[name]', with: 'Test'
129
+ find('button[type="submit"]').click
130
+ end
131
+ expect(label.name).to be == 'Label editing test'
132
+ expect(page).to have_content 'Name has already been taken'
133
+ end
134
+
135
+ it 'must not be updated without valid parameters' do
136
+ within find('.edit_label') do
137
+ fill_in 'label[color]', with: ''
138
+ find('button[type="submit"]').click
139
+ end
140
+ expect(label.color).not_to be == ''
141
+ expect(page).to have_content "Color can't be blank"
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+
148
+ describe 'destroy' do
149
+
150
+ let!(:label) {create(:label)}
151
+ let!(:variant) do
152
+ variant = create(:variant)
153
+ variant.labels << label
154
+ 2.times do
155
+ l = create(:label)
156
+ variant.labels << l
157
+ end
158
+ variant.save
159
+ variant
160
+ end
161
+
162
+ it 'must remove the deleted label from any variants who has it' do
163
+ visit spree.admin_labels_path
164
+ within first('tr[data-hook="admin_labels_index_rows"]') do
165
+ find('a[data-action="remove"]').click
166
+ end
167
+ expect(page).to have_content 'successfully removed!'
168
+ expect(variant.labels.count).to be == 2
169
+ end
170
+
171
+ end
172
+
173
+
174
+ end