Class: Spree::Billing::BusinessesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/billing/businesses_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#current_vendor, #default_url_options, #edit_object_url, #handle_unauthorized_vendor, #page, #per_page, #required_vendor_user!, #set_locale, #switch_vendor, #vendors

Methods included from SpreeCmCommissioner::Billing::RoleAuthorization

#auth_action, #auth_entry, #auth_user, #authorize!, #authorize?, #authorize_admin, #authorize_role!, #redirect_unauthorized_access

Instance Method Details

#assign_parent(parent_id) ⇒ Object



85
86
87
# File 'app/controllers/spree/billing/businesses_controller.rb', line 85

def assign_parent(parent_id)
  @taxon.parent = current_store.taxons.find(parent_id) if parent_id
end

#collection_urlObject



114
115
116
# File 'app/controllers/spree/billing/businesses_controller.rb', line 114

def collection_url
  spree.polymorphic_url([:billing, business.to_sym], options)
end

#edit_billing_taxon_path(taxon) ⇒ Object



118
119
120
# File 'app/controllers/spree/billing/businesses_controller.rb', line 118

def edit_billing_taxon_path(taxon)
  spree.edit_billing_taxonomy_taxon_path(taxon.taxonomy, taxon.id)
end

#indexObject



14
# File 'app/controllers/spree/billing/businesses_controller.rb', line 14

def index; end

#load_businesses_taxonomyObject



7
8
9
# File 'app/controllers/spree/billing/businesses_controller.rb', line 7

def load_businesses_taxonomy
  @businesses_taxonomy = Spree::Taxonomy.businesses
end

#location_after_saveObject



58
59
60
# File 'app/controllers/spree/billing/businesses_controller.rb', line 58

def location_after_save
  spree.billing_businesses_url
end

#model_classObject



126
127
128
# File 'app/controllers/spree/billing/businesses_controller.rb', line 126

def model_class
  Spree::Taxon
end

#newObject



16
17
18
19
# File 'app/controllers/spree/billing/businesses_controller.rb', line 16

def new
  @taxon = Spree::Taxon.new
  @taxon.taxonomy = @businesses_taxonomy
end

#object_nameObject



130
131
132
# File 'app/controllers/spree/billing/businesses_controller.rb', line 130

def object_name
  'taxon'
end

#parent_dataObject



89
90
91
92
93
94
95
# File 'app/controllers/spree/billing/businesses_controller.rb', line 89

def parent_data
  if action_name == 'index'
    nil
  else
    super
  end
end


103
104
105
106
# File 'app/controllers/spree/billing/businesses_controller.rb', line 103

def regenerate_permalink
  reload_taxon_and_set_permalink(@taxon)
  @update_children = true
end


108
109
110
111
112
# File 'app/controllers/spree/billing/businesses_controller.rb', line 108

def reload_taxon_and_set_permalink(taxon)
  taxon.reload
  taxon.set_permalink
  taxon.save!
end

#rename_child_taxonsObject



97
98
99
100
101
# File 'app/controllers/spree/billing/businesses_controller.rb', line 97

def rename_child_taxons
  @taxon.descendants.each do |taxon|
    reload_taxon_and_set_permalink(taxon)
  end
end

#scopeObject



122
123
124
# File 'app/controllers/spree/billing/businesses_controller.rb', line 122

def scope
  current_store.taxonomies
end


68
69
70
71
72
73
74
# File 'app/controllers/spree/billing/businesses_controller.rb', line 68

def set_permalink_params
  set_permalink_part

  return unless params.key? 'permalink_part'

  params[:taxon][:permalink] = @parent_permalink + params[:permalink_part]
end


62
63
64
65
66
# File 'app/controllers/spree/billing/businesses_controller.rb', line 62

def set_permalink_part
  @permalink_part = @object.permalink.split('/').last
  @parent_permalink = @object.permalink.split('/')[0...-1].join('/')
  @parent_permalink += '/' if @parent_permalink.present?
end

#set_positionObject



80
81
82
83
# File 'app/controllers/spree/billing/businesses_controller.rb', line 80

def set_position
  new_position = params[:taxon][:position]
  @taxon.child_index = new_position.to_i if new_position
end

#taxon_paramsObject



76
77
78
# File 'app/controllers/spree/billing/businesses_controller.rb', line 76

def taxon_params
  params.require(:taxon).permit(:stop, :station, :branch, :location, permitted_taxon_attributes)
end

#updateObject

rubocop:disable Metrics/AbcSize



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/spree/billing/businesses_controller.rb', line 21

def update # rubocop:disable Metrics/AbcSize
  successful = @taxon.transaction do
    parent_id = params[:taxon][:parent_id]
    set_position
    assign_parent(parent_id)

    @taxon.save!

    # regenerate permalink
    regenerate_permalink if parent_id

    set_permalink_params

    # check if we need to rename child taxons if parent name or permalink changes
    @update_children = true if params[:taxon][:name] != @taxon.name || params[:taxon][:permalink] != @taxon.permalink

    @taxon.create_icon(attachment: taxon_params[:icon]) if taxon_params[:icon]
    @taxon.update(taxon_params.except(:icon))
  end
  if successful
    flash[:success] = flash_message_for(@taxon, :successfully_updated)

    # rename child taxons
    rename_child_taxons if @update_children

    respond_with(@taxon) do |format|
      format.html { redirect_to spree.edit_billing_business_url(params[:id]) }
      format.json { render json: @taxon.to_json }
    end
  else
    respond_with(@taxon) do |format|
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @taxon.errors.full_messages.to_sentence, status: :unprocessable_entity }
    end
  end
end