deal_redemptions 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 381dc98f7c647f4aff7871865ab7f994fdfb7cc5
4
- data.tar.gz: f7a05a51e13ffdd9fb2e01ea5b55eba95d5745d5
3
+ metadata.gz: fa79c20901b82ad1ece7615c38436ec366691059
4
+ data.tar.gz: 21d77a4b1aca8f9c5ba4255d307f1f05f6e78533
5
5
  SHA512:
6
- metadata.gz: acee070426e553537996a592b6e388fa7c48feed5f3666123da8f9dbbd8d663620d25cf196cb503f6e2cbae247e8d959855baaa542e7ed20929fff2fa05db8de
7
- data.tar.gz: 05938303b452f8a14315dbe3e2822a9ff48f9b8d20c7144b8010b04d1880ab6edea753de0d160f8060b75db81934609e3d37d6901bde528772899a53244bc5df
6
+ metadata.gz: 6035d4040643b51a627ecf86a08825247f071e4474e2a85b6eb2d12e173b8bf184beebfcf25d417a4eea1cd8dc50ded52537add967b148d8d36f1382f1eb7c3e
7
+ data.tar.gz: a8b1cf5705378a0844e7b2ddf736477998d19ca6e7e6debe6fe05c585c557e1a402ca71e9e59d4462900e63b9e316aef5f8e8973b1c812b0d262d378594091e0
@@ -32,9 +32,14 @@ class Navigation
32
32
  $('section#mobile-navigation select').bind 'change', () ->
33
33
  window.location.href = @.value
34
34
 
35
+ class RedeemCode
36
+ generate_slug: (string) ->
37
+ string.replace(/\W+/g, '-').toLowerCase()
38
+
35
39
 
36
40
  $(document).ready ->
37
41
  navigation = new Navigation
42
+ redeem_code = new RedeemCode
38
43
 
39
44
  # Responsive mobile navigation
40
45
  navigation.mobile()
@@ -74,4 +79,10 @@ $(document).ready ->
74
79
 
75
80
  }).on('changeDate', (ev) ->
76
81
  checkout.hide()
77
- ).data('datepicker')
82
+ ).data('datepicker')
83
+
84
+ # Generate company slug
85
+ $('input#company_name').keyup(() ->
86
+ slug = $('input#company_slug')
87
+ slug.val(redeem_code.generate_slug(@.value))
88
+ )
@@ -0,0 +1,107 @@
1
+ require_dependency "deal_redemptions/application_controller"
2
+
3
+ module DealRedemptions
4
+ class Admin::RedeemCodesController < ApplicationController
5
+ layout 'deal_redemptions/admin/default'
6
+
7
+ before_action :admin_authorize
8
+ before_action :set_admin_redeem_code, only: [:edit, :update, :destroy]
9
+ before_action :check_existing_company_code, only: [:update, :create]
10
+
11
+ # GET /admin/redeem_codes
12
+ def index
13
+ # Check if search param is present
14
+ if params[:search].blank?
15
+ @admin_redeem_codes = DealRedemptions::RedeemCode.page(params[:page]).order(:code).includes(:company)
16
+ else
17
+ query = DealRedemptions::Redemption.find_by_sql(build_search_query)
18
+ @admin_redeem_codes = Kaminari.paginate_array(query).page(params[:page])
19
+ end
20
+ end
21
+
22
+ # GET /admin/redeem_codes/new
23
+ def new
24
+ @admin_redeem_code = DealRedemptions::RedeemCode.new
25
+ end
26
+
27
+ # GET /admin/redeem_codes/1/edit
28
+ def edit
29
+ end
30
+
31
+ # POST /admin/redeem_codes
32
+ def create
33
+ @admin_redeem_code = DealRedemptions::RedeemCode.new(redeem_code_params)
34
+
35
+ if @existing_code.count > 0
36
+ flash[:notice] = 'Redemption code already exists.'
37
+ render :new
38
+ else
39
+ if @admin_redeem_code.save
40
+ redirect_to admin_redeem_codes_path, notice: 'Redeem code was successfully created.'
41
+ else
42
+ render :new
43
+ end
44
+ end
45
+ end
46
+
47
+ # PATCH/PUT /admin/redeem_codes/1
48
+ def update
49
+ if @existing_code.count > 0
50
+ flash[:notice] = 'Redemption code already exists.'
51
+ render :edit
52
+ else
53
+ if @admin_redeem_code.update(redeem_code_params)
54
+ redirect_to admin_redeem_codes_path, notice: 'Redeem code was successfully updated.'
55
+ else
56
+ render :edit
57
+ end
58
+ end
59
+ end
60
+
61
+ # DELETE /admin/redeem_codes/1
62
+ def destroy
63
+ @admin_redeem_code.destroy
64
+ redirect_to admin_redeem_codes_url, notice: 'Redeem code was successfully destroyed.'
65
+ end
66
+
67
+ private
68
+ # Use callbacks to share common setup or constraints between actions.
69
+ def set_admin_redeem_code
70
+ @admin_redeem_code = DealRedemptions::RedeemCode.find(params[:id])
71
+ end
72
+
73
+ # Check existing redemption code by company
74
+ def check_existing_company_code
75
+ redeem_code = params[:redeem_code]
76
+ @existing_code = DealRedemptions::RedeemCode.find_code_and_company(redeem_code[:code], redeem_code[:company_id])
77
+ end
78
+
79
+ # Search redemption codes
80
+ def build_search_query
81
+ string = params[:search].split ' '
82
+ redeem_codes = DealRedemptions::RedeemCode.arel_table
83
+ companies = DealRedemptions::Company.arel_table
84
+ query = redeem_codes.project(
85
+ redeem_codes[:id],
86
+ redeem_codes[:company_id],
87
+ redeem_codes[:code],
88
+ redeem_codes[:status],
89
+ redeem_codes[:created_at]
90
+ )
91
+ .join(companies)
92
+ .on(redeem_codes[:company_id]
93
+ .eq(companies[:id]))
94
+
95
+ string.each do |s|
96
+ query.where(redeem_codes[:code].matches("%#{s}%"))
97
+ end
98
+
99
+ query.to_sql
100
+ end
101
+
102
+ # Only allow a trusted parameter "white list" through.
103
+ def redeem_code_params
104
+ params.require(:redeem_code).permit(:company_id, :product_id, :code, :status)
105
+ end
106
+ end
107
+ end
@@ -26,5 +26,10 @@ module DealRedemptions
26
26
  def self.by_company(company)
27
27
  where(company_id: company)
28
28
  end
29
+
30
+ # Find code and company
31
+ def self.find_code_and_company(code, company)
32
+ where("code = ? AND company_id = ?", code, company)
33
+ end
29
34
  end
30
35
  end
@@ -0,0 +1,22 @@
1
+ - if @admin_redeem_code.errors.any?
2
+ .alert.alert-danger
3
+ %ul
4
+ - @admin_redeem_code.errors.full_messages.each do |msg|
5
+ %li
6
+ = msg
7
+
8
+ = form_for [:admin, @admin_redeem_code] do |f|
9
+ .form-group
10
+ = f.label :code
11
+ = f.text_field :code, class: 'form-control'
12
+ .form-group
13
+ = f.label :company_id, 'Company'
14
+ = f.select :company_id, DealRedemptions::Company.all_by_name.collect {|c| [c.name, c.id]}, {}, { class: 'form-control' }
15
+ .form-group
16
+ = f.label :product_id, 'Product'
17
+ = f.select :product_id, DealRedemptions::Product.all.collect {|p| [p.name, p.id]}, {}, { class: 'form-control' }
18
+ .form-group
19
+ = f.label :status
20
+ = f.select :status, [:active, :redeemed, :void], {}, { class: 'form-control' }
21
+ .form-group.pull-right
22
+ = f.submit @admin_redeem_code.new_record? ? "Create Code" : "Edit Code", class: 'btn btn-primary'
@@ -0,0 +1,22 @@
1
+ - page_title "Redemption Code"
2
+ - admin_page_title "<i class=\"redemptions\"></i> #{link_to 'Redemption Codes', admin_redeem_code_path} / #{@admin_redeem_code.code}".html_safe
3
+
4
+ .row
5
+ .col-md-4
6
+ %h3 Edit existing redemption code
7
+ Make modifications to an existing redemption code.
8
+ %hr
9
+
10
+ - unless @admin_redeem_code.redemption_id.blank?
11
+ %h4 This code has been used.
12
+ = link_to 'View Redemption', admin_redemption_path(@admin_redeem_code.redemption_id)
13
+ %hr
14
+
15
+ %h4 Want to remove this code?
16
+ %h5 Click the button below. You can <strong>NOT</strong> undo this action.
17
+
18
+ = button_to 'Delete Redeem Code', admin_redeem_code_path, method: :delete, name: nil, class: 'btn btn-danger', onclick: 'if(!confirm("Are you sure you want to delete this redemption code?")) return false;'
19
+ %hr
20
+
21
+ .col-md-8
22
+ = render 'form'
@@ -0,0 +1,55 @@
1
+ - page_title 'Redemption Codes'
2
+ - admin_page_title '<i class="redemptions"></i> Redemption Codes'.html_safe
3
+
4
+ %hr
5
+ .row.clearfix
6
+ .col-md-4
7
+ = form_tag '', method: 'get', class: 'search-redemptions pull-right' do
8
+ .col-md-8.padding-right-5
9
+ .form-group
10
+ = search_field_tag '', '', name: 'search', class: 'form-control', placeholder: 'Search by code:'
11
+ .col-md-4.padding-left-5
12
+ .form-group
13
+ = button_tag 'Search', name: nil, class: 'btn btn-primary'
14
+ .col-md-8
15
+ %hr
16
+
17
+ - if @admin_redeem_codes.empty?
18
+
19
+ .no-current-redemptions
20
+ %i.redemptions.clearfix
21
+ No redemption codes were found
22
+
23
+ - else
24
+ .row
25
+ .col-md-12
26
+ = link_to 'New Code', new_admin_redeem_code_path, class: 'btn btn-primary pull-right'
27
+ %hr
28
+
29
+ = paginate @admin_redeem_codes
30
+
31
+ %table{id: 'redemptions', class: 'table default'}
32
+ %thead
33
+ %tr
34
+ %td
35
+ Code
36
+ %td
37
+ Company:
38
+ %td
39
+ Status:
40
+ %td
41
+ Created:
42
+ %tbody
43
+ - @admin_redeem_codes.each do |code|
44
+ %tr
45
+ %td
46
+ = link_to "#{code.code}", edit_admin_redeem_code_path(code.id)
47
+ %td
48
+ = code.company.name
49
+ %td
50
+ = code.status
51
+ %td
52
+ = code.created_at
53
+
54
+ .row-fluid
55
+ = paginate @admin_redeem_codes
@@ -0,0 +1,11 @@
1
+ - page_title "New Redemption Code"
2
+ - admin_page_title "<i class=\"redemptions\"></i> #{link_to 'Redeem Codes', admin_redeem_codes_path} / New Redeem Code".html_safe
3
+
4
+ .row
5
+ .col-md-4
6
+ %h3 New redemption code
7
+ Add new deal site redemption code.
8
+ %hr
9
+
10
+ .col-md-8
11
+ = render 'form'
@@ -10,6 +10,8 @@
10
10
  = admin_link_to('<i class="products"></i> Products'.html_safe, admin_products_path, {class: 'sidebar-link'})
11
11
  %li
12
12
  = admin_link_to('<i class="companies"></i> Companies'.html_safe, admin_companies_path, {class: 'sidebar-link'})
13
+ %li
14
+ = admin_link_to('<i class="redemptions"></i> Redeem Codes'.html_safe, admin_redeem_codes_path, {class: 'sidebar-link'})
13
15
  %li
14
16
  = admin_link_to('<i class="import"></i> Import/Export'.html_safe, admin_import_path, {class: 'sidebar-link'})
15
17
  %li
@@ -5,4 +5,4 @@
5
5
  -# per_page: number of items to fetch per page
6
6
  -# remote: data-remote
7
7
  %li.page.gap
8
- = t('views.pagination.truncate').html_safe
8
+ =# t('views.pagination.truncate').html_safe
@@ -36,6 +36,7 @@
36
36
  <option value="<%= admin_redemptions_path %>">Redemptions</option>
37
37
  <option value="<%= admin_products_path %>">Products</option>
38
38
  <option value="<%= admin_companies_path %>">Companies</option>
39
+ <option value="<%= admin_redeem_codes_path %>">Redeem Codes</option>
39
40
  <option value="<%= admin_import_path %>">Import/Export</option>
40
41
  <option value="<%= admin_settings_path %>">Settings</option>
41
42
  </select>
data/config/routes.rb CHANGED
@@ -33,6 +33,7 @@ DealRedemptions::Engine.routes.draw do
33
33
  resources :companies
34
34
  resources :redemptions
35
35
  resources :products, except: [:show]
36
+ resources :redeem_codes, except: [:show]
36
37
  resources :sessions
37
38
  end
38
39
  end
@@ -1,3 +1,3 @@
1
1
  module DealRedemptions
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,143 @@
1
+ require 'rails_helper'
2
+
3
+ module DealRedemptions
4
+ RSpec.describe Admin::RedeemCodesController, :type => :controller do
5
+ routes { DealRedemptions::Engine::routes }
6
+
7
+ # This should return the minimal set of attributes required to create a valid
8
+ # Admin::RedeemCode. As you add validations to Admin::RedeemCode, be sure to
9
+ # adjust the attributes here as well.
10
+ let(:valid_attributes) {
11
+ FactoryGirl.attributes_for(:redeem_code)
12
+ }
13
+
14
+ let(:invalid_attributes) {
15
+ FactoryGirl.attributes_for(:redeem_code, code: '')
16
+ }
17
+
18
+ # This should return the minimal set of values that should be in the session
19
+ # in order to pass any filters (e.g. authentication) defined in
20
+ # Admin::RedeemCodesController. Be sure to keep this updated too.
21
+ let(:valid_session) {
22
+ {admin_user_id: 1}
23
+ }
24
+
25
+ before :each do
26
+ FactoryGirl.create(:user, id: 1)
27
+
28
+ # Mock call to check for existing redemption code on create or update
29
+ allow(DealRedemptions::RedeemCode).to receive(:find_code_by_company).and_return([])
30
+ end
31
+
32
+ describe "GET index" do
33
+ it "assigns all admin_redeem_codes as @admin_redeem_codes" do
34
+ redeem_code = FactoryGirl.create(:redeem_code)
35
+ get :index, {}, valid_session
36
+ expect(assigns(:admin_redeem_codes)).to eq([redeem_code])
37
+ end
38
+ end
39
+
40
+ describe "GET new" do
41
+ it "assigns a new admin_redeem_code as @admin_redeem_code" do
42
+ get :new, {}, valid_session
43
+ expect(assigns(:admin_redeem_code)).to be_a_new(DealRedemptions::RedeemCode)
44
+ end
45
+ end
46
+
47
+ describe "GET edit" do
48
+ it "assigns the requested admin_redeem_code as @admin_redeem_code" do
49
+ redeem_code = FactoryGirl.create(:redeem_code)
50
+ get :edit, {:id => redeem_code.to_param}, valid_session
51
+ expect(assigns(:admin_redeem_code)).to eq(redeem_code)
52
+ end
53
+ end
54
+
55
+ describe "POST create" do
56
+ describe "with valid params" do
57
+ it "creates a new Admin::RedeemCode" do
58
+ expect {
59
+ post :create, {:redeem_code => valid_attributes}, valid_session
60
+ }.to change(DealRedemptions::RedeemCode, :count).by(1)
61
+ end
62
+
63
+ it "assigns a newly created admin_redeem_code as @admin_redeem_code" do
64
+ post :create, {:redeem_code => valid_attributes}, valid_session
65
+ expect(assigns(:admin_redeem_code)).to be_a(DealRedemptions::RedeemCode)
66
+ expect(assigns(:admin_redeem_code)).to be_persisted
67
+ end
68
+
69
+ it "redirects to the admin_redeem_codes" do
70
+ post :create, {:redeem_code => valid_attributes}, valid_session
71
+ expect(response).to redirect_to(admin_redeem_codes_path)
72
+ end
73
+ end
74
+
75
+ describe "with invalid params" do
76
+ it "assigns a newly created but unsaved admin_redeem_code as @admin_redeem_code" do
77
+ post :create, {:redeem_code => invalid_attributes}, valid_session
78
+ expect(assigns(:admin_redeem_code)).to be_a_new(DealRedemptions::RedeemCode)
79
+ end
80
+
81
+ it "re-renders the 'new' template" do
82
+ post :create, {:redeem_code => invalid_attributes}, valid_session
83
+ expect(response).to render_template("new")
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "PUT update" do
89
+ describe "with valid params" do
90
+ let(:new_attributes) {
91
+ { redeem_code: FactoryGirl.attributes_for(:redeem_code) }
92
+ }
93
+
94
+ before :each do
95
+ @redeem_code = FactoryGirl.create(:redeem_code)
96
+ put :update, {:id => @redeem_code.to_param, :redeem_code => valid_attributes}, valid_session
97
+ end
98
+
99
+ it "updates the requested admin_redeem_code" do
100
+ @redeem_code.reload
101
+ expect(@redeem_code.id).to eq(1)
102
+ end
103
+
104
+ it "assigns the requested admin_redeem_code as @admin_redeem_code" do
105
+ expect(assigns(:admin_redeem_code)).to eq(@redeem_code)
106
+ end
107
+ end
108
+
109
+ describe "with invalid params" do
110
+ before :each do
111
+ @redeem_code = FactoryGirl.create(:redeem_code)
112
+ put :update, {:id => @redeem_code.to_param, :redeem_code => invalid_attributes}, valid_session
113
+ end
114
+
115
+ it "assigns the admin_redeem_code as @admin_redeem_code" do
116
+ expect(assigns(:admin_redeem_code)).to eq(@redeem_code)
117
+ end
118
+
119
+ it "re-renders the 'edit' template" do
120
+ expect(response).to render_template("edit")
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "DELETE destroy" do
126
+ before :each do
127
+ @redeem_code = FactoryGirl.create(:redeem_code)
128
+ end
129
+
130
+ it "destroys the requested admin_redeem_code" do
131
+ expect {
132
+ delete :destroy, {:id => @redeem_code.to_param}, valid_session
133
+ }.to change(DealRedemptions::RedeemCode, :count).by(-1)
134
+ end
135
+
136
+ it "redirects to the admin_redeem_codes list" do
137
+ delete :destroy, {:id => @redeem_code.to_param}, valid_session
138
+ expect(response).to redirect_to(admin_redeem_codes_url)
139
+ end
140
+ end
141
+
142
+ end
143
+ end