Class: Spree::Transit::PlacesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/transit/places_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#current_vendor, #edit_object_url, #page, #per_page, #required_vendor_user!, #vendors

Instance Method Details

#assign_parent(parent_id) ⇒ Object



101
102
103
# File 'app/controllers/spree/transit/places_controller.rb', line 101

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

#collection_urlObject



130
131
132
# File 'app/controllers/spree/transit/places_controller.rb', line 130

def collection_url
  spree.polymorphic_url([:transit, place.to_sym], options)
end

#edit_transit_taxon_path(taxon) ⇒ Object



134
135
136
# File 'app/controllers/spree/transit/places_controller.rb', line 134

def edit_transit_taxon_path(taxon)
  spree.edit_transit_taxonomy_taxon_path(taxon.taxonomy, taxon.id)
end

#indexObject



16
# File 'app/controllers/spree/transit/places_controller.rb', line 16

def index; end

#load_place_taxonomyObject



7
8
9
# File 'app/controllers/spree/transit/places_controller.rb', line 7

def load_place_taxonomy
  @place_taxonomy = Spree::Taxonomy.place
end

#location_after_saveObject



60
61
62
# File 'app/controllers/spree/transit/places_controller.rb', line 60

def location_after_save
  spree.transit_places_url
end

#model_classObject



142
143
144
# File 'app/controllers/spree/transit/places_controller.rb', line 142

def model_class
  Spree::Taxon
end

#newObject



18
19
20
21
# File 'app/controllers/spree/transit/places_controller.rb', line 18

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

#object_nameObject



146
147
148
# File 'app/controllers/spree/transit/places_controller.rb', line 146

def object_name
  'taxon'
end

#parent_dataObject



105
106
107
108
109
110
111
# File 'app/controllers/spree/transit/places_controller.rb', line 105

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


119
120
121
122
# File 'app/controllers/spree/transit/places_controller.rb', line 119

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


124
125
126
127
128
# File 'app/controllers/spree/transit/places_controller.rb', line 124

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

#rename_child_taxonsObject



113
114
115
116
117
# File 'app/controllers/spree/transit/places_controller.rb', line 113

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

#scopeObject



138
139
140
# File 'app/controllers/spree/transit/places_controller.rb', line 138

def scope
  current_store.taxonomies
end

#set_data_typeObject

If this method is applied to the model, it will set stop, location, and taxon attributes to 0 when a drag-and-drop action changes the position.



89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/spree/transit/places_controller.rb', line 89

def set_data_type
  place_types = params[:taxon]
  if place_types[:stop].to_i.zero? && place_types[:station].to_i.zero? && place_types[:branch].to_i.zero?
    # If not checked, it will set the location instead.
    place_types[:location] = 1
    @taxon[:data_type] = (place_types[:location].to_i * (2**3))
  end

  result = (place_types[:stop].to_i * (2**0)) + (place_types[:station].to_i * (2**1)) + (place_types[:branch].to_i * (2**2)) + (place_types[:location].to_i * (2**3)) # rubocop:disable Layout/LineLength
  @taxon[:data_type] = result
end


70
71
72
73
74
75
76
# File 'app/controllers/spree/transit/places_controller.rb', line 70

def set_permalink_params
  set_permalink_part

  return unless params.key? 'permalink_part'

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


64
65
66
67
68
# File 'app/controllers/spree/transit/places_controller.rb', line 64

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



82
83
84
85
# File 'app/controllers/spree/transit/places_controller.rb', line 82

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

#taxon_paramsObject



78
79
80
# File 'app/controllers/spree/transit/places_controller.rb', line 78

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

#updateObject

rubocop:disable Metrics/AbcSize



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
57
58
# File 'app/controllers/spree/transit/places_controller.rb', line 23

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_transit_place_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