Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

Commit

Permalink
Depracate self.current method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Tapia committed Nov 24, 2018
1 parent fd6ab10 commit 28ff366
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 65 deletions.
26 changes: 10 additions & 16 deletions app/models/spree/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Spree::Tracker < ActiveRecord::Base
def self.current(store = nil)
return if !store
if store.is_a?(Spree::Store)
Spree::Tracker.where(active: true, store_id: store)
Spree::Tracker.where(active: true, store_id: store).first
else
# TODO: Remove in 2.0
ActiveSupport::Deprecation.warn <<-EOS.squish, caller
Expand All @@ -16,24 +16,18 @@ def self.current(store = nil)
Spree::Tracker.where(active: true).joins(:store).where(
"spree_stores.code = ? OR spree_stores.url LIKE ?",
store, "%#{store}%"
)
).first
end
end
class << self
deprecate current_tracker: :current, deprecator: Spree::Deprecation
end

def self.by_type(store = nil, type = nil)
def self.by_type(store: Spree::Store.default, type: 'google_analytics')
return if !store
if store.is_a?(Spree::Store)
Spree::Tracker.where(active: true, store_id: store, tracker_type: type).first
else
# TODO: Remove in 2.0
ActiveSupport::Deprecation.warn <<-EOS.squish, caller
Calling Spree::Tracker.by_type with a string is DEPRECATED. Instead
pass it an instance of Spree::Store.
EOS
Spree::Tracker.where(active: true, tracker_type: type).joins(:store).where(
"spree_stores.code = ? OR spree_stores.url LIKE ?",
store, "%#{store}%"
).first
end

Spree::Tracker.where(active: true,
store_id: store,
tracker_type: type).first
end
end
4 changes: 2 additions & 2 deletions app/views/spree/admin/trackers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
<ul>
<li>
<%= radio_button(:tracker, :active, true) %>
<%= label_tag :tracker_active_true, Spree.t(:say_yes) %>
<%= label_tag :tracker_active_true, t('spree.say_yes') %>
</li>
<li>
<%= radio_button(:tracker, :active, false) %>
<%= label_tag :tracker_active_false, Spree.t(:say_no) %>
<%= label_tag :tracker_active_false, t('Spree.say_no') %>
</li>
</ul>
</div>
Expand Down
41 changes: 24 additions & 17 deletions app/views/spree/admin/trackers/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
<%= render 'spree/admin/shared/general_tabs' %>
<%= render "spree/admin/shared/general_tabs" %>
<% admin_breadcrumb(Spree.t(:settings)) %>
<% admin_breadcrumb(Spree.t(:general_settings)) %>
<% admin_breadcrumb(t("spree.settings")) %>
<% admin_breadcrumb(t("spree.general_settings")) %>
<% admin_breadcrumb(plural_resource_name(Spree::Tracker)) %>
<% content_for :page_actions do %>
<% if can?(:create, Spree::Tracker) %>
<li>
<%= link_to Spree.t(:new_tracker), new_object_url, id: 'admin_new_tracker_link', class: 'btn btn-primary' %>
<%= link_to t("spree.new_tracker"),
new_object_url,
:id => "admin_new_tracker_link",
:class => "btn btn-primary" %>
</li>
<% end %>
<% end %>
<% if @trackers.any? %>
<table class='index'>
<table class="index">
<colgroup>
<col style='width: 20%'>
<col style='width: 10%'>
<col style='width: 5%'>
<col style='width: 25%'>
<col style='width: 10%'>
<col style="width: 20%">
<col style="width: 10%">
<col style="width: 5%">
<col style="width: 25%">
<col style="width: 10%">
</colgroup>

<thead>
<tr data-hook='admin_trackers_index_headers'>
<tr data-hook="admin_trackers_index_headers">
<th><%= Spree::Tracker.human_attribute_name(:analytics_id) %></th>
<th><%= Spree::Tracker.human_attribute_name(:tracker_type) %></th>
<th class='align-center'><%= Spree::Tracker.human_attribute_name(:active) %></th>
<th><%= Spree.t(:store) %></th>
<th><%= t("spree.status") %></th>
<th><%= t("spree.store") %></th>
<th class="actions"></th>
</tr>
</thead>

<tbody>
<% @trackers.each do |tracker|%>
<tr id='<%= spree_dom_id tracker %>'' data-hook='admin_trackers_index_rows' class='<%= cycle('odd', 'even')%>'>
<tr id="<%= spree_dom_id tracker %>" data-hook="admin_trackers_index_rows" class="<%= cycle("odd", "even")%>">
<td><%= tracker.analytics_id %></td>
<td><%= display_tracker_type(tracker) %></td>
<td class='align-center'><%= tracker.active ? Spree.t(:say_yes) : Spree.t(:say_no) %></td>
<td>
<span class="pill pill-<%= tracker.active ? 'active' : 'inactive' %>">
<%= t(tracker.active? ? :active : :inactive, scope: 'spree') %>
</span>
</td>
<td><%= tracker.store.try(:name) %></td>
<td class="actions">
<% if can?(:update, tracker) %>
Expand All @@ -53,8 +60,8 @@
</tbody>
</table>
<% else %>
<div class='no-objects-found'>
<%= render 'spree/admin/shared/no_objects_found',
<div class="no-objects-found">
<%= render "spree/admin/shared/no_objects_found",
resource: Spree::Tracker,
new_resource_url: new_object_url %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/trackers/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= render 'spree/admin/shared/general_tabs' %>
<% admin_breadcrumb(link_to plural_resource_name(Spree::Tracker), spree.admin_trackers_path) %>
<% admin_breadcrumb(Spree.t(:new_tracker)) %>
<% admin_breadcrumb(t("spree.new_tracker")) %>
<% content_for :page_actions do %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/shared/_google_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% tracker = Spree::Tracker.by_type(Spree::Store.default, 'google_analytics') %>
<% tracker = Spree::Tracker.by_type(store: Spree::Store.default, type: 'google_analytics') %>
<% if tracker %>
<script id='solidus_trackers_google_analytics'>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/shared/_google_manager.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% tracker = Spree::Tracker.by_type(Spree::Store.default, 'google_manager') %>
<% tracker = Spree::Tracker.by_type(store: Spree::Store.default, type: 'google_manager') %>
<% if tracker %>
<script id='solidus_trackers_google_manager'>
Expand Down
6 changes: 3 additions & 3 deletions spec/features/admin/configuration/analytics_tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
within_row(1) do
expect(column_text(1)).to eq("A100")
expect(column_text(2)).to eq("Google Analytics")
expect(column_text(3)).to eq("Yes")
expect(column_text(3)).to eq("Active")
end

within_row(2) do
expect(column_text(1)).to eq("A100")
expect(column_text(2)).to eq("Google Analytics")
expect(column_text(3)).to eq("Yes")
expect(column_text(3)).to eq("Active")
end
end
end
Expand All @@ -49,7 +49,7 @@
within_row(1) do
expect(column_text(1)).to eq("A100")
expect(column_text(2)).to eq("Google Analytics")
expect(column_text(3)).to eq("Yes")
expect(column_text(3)).to eq("Active")
end
end
end
Expand Down
38 changes: 14 additions & 24 deletions spec/models/spree/tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Spree::Tracker, type: :model do
let!(:store) { create(:store) }
let!(:tracker) { create(:tracker, store: store) }
let!(:tracker) { create(:tracker, store: store, tracker_type: 'google_manager') }
let(:other_store) { create(:store, code: 'STORE2', url: 'realfakedoors.com') }
let!(:other_tracker) { create(:tracker, store: other_store) }

Expand All @@ -12,63 +12,53 @@
end

it "returns the first active tracker" do
expect(Spree::Tracker.current(store)).to eq([tracker])
expect(Spree::Tracker.current(store)).to eq(tracker)
end

it "does not return an inactive tracker" do
tracker.update_attribute(:active, false)
expect(Spree::Tracker.current(store)).to eq([])
expect(Spree::Tracker.current(store)).to eq(nil)
end

it "finds tracker by store" do
expect(Spree::Tracker.current(store)).to eq([tracker])
expect(Spree::Tracker.current(store)).to eq(tracker)
end

it "finds tracker based on store code" do
aggregate_failures do
expect(ActiveSupport::Deprecation).to receive(:warn)
expect(Spree::Tracker.current('STORE2')).to eq([other_tracker])
expect(Spree::Tracker.current('STORE2')).to eq(other_tracker)
end
end

it "finds tracker based on store url" do
aggregate_failures do
expect(ActiveSupport::Deprecation).to receive(:warn)
expect(Spree::Tracker.current(store.url)).to eq([tracker])
expect(Spree::Tracker.current(store.url)).to eq(tracker)
end
end
end

describe "by_type" do
it "returns nil if no store passed in argument" do
expect(Spree::Tracker.by_type).to eq(nil)
expect(Spree::Tracker.by_type(store: nil, type: nil)).to eq(nil)
end

it "returns the first active tracker" do
expect(Spree::Tracker.by_type(store, tracker.tracker_type)).to eq(tracker)
expect(Spree::Tracker.by_type(store: store,
type: tracker.tracker_type)).to eq(tracker)
end

it "does not return an inactive tracker" do
tracker.update_attribute(:active, false)
expect(Spree::Tracker.by_type(store, tracker.tracker_type)).to eq(nil)
expect(Spree::Tracker.by_type(store: store,
type: tracker.tracker_type)).to eq(nil)
end

it "finds tracker by store" do
expect(Spree::Tracker.by_type(store, tracker.tracker_type)).to eq(tracker)
end

it "finds tracker based on store code" do
aggregate_failures do
expect(ActiveSupport::Deprecation).to receive(:warn)
expect(Spree::Tracker.by_type('STORE2', tracker.tracker_type)).to eq(other_tracker)
end
end

it "finds tracker based on store url" do
aggregate_failures do
expect(ActiveSupport::Deprecation).to receive(:warn)
expect(Spree::Tracker.by_type(store.url, tracker.tracker_type)).to eq(tracker)
end
tracker.update_attribute(:tracker_type, 'google_analytics')
expect(Spree::Tracker.by_type(store: store,
type: tracker.tracker_type)).to eq(tracker)
end
end
end

0 comments on commit 28ff366

Please sign in to comment.