Module: WeBridge::AutoViewHelper::Controller

Defined in:
lib/we_bridge/auto_view_helper/controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 15

def self.included(base)
  base.extend ClassMethods
  base.before_action :set_record, only: [:show, :edit, :update, :destroy]
end

Instance Method Details

#createObject

POST /???s



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 39

def create
  @record = self.class.model.new(record_params)
  @record.class.transaction do
    @record.save!
    record_text_params.try :each do |code,text_params|
      text = @record.text(code)
      text.attributes = text_params
      text.save!
    end
    flash[:notice] = 'Record was successfully created.'
    redirect_to action: :show, id: @record.id
  end
rescue
  redirect_to action: :new
end

#destroyObject

DELETE /???s/1



74
75
76
77
78
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 74

def destroy
  @record.destroy
  flash[:notice]= 'Record was successfully destroyed.'
  redirect_to action: :index
end

#editObject

GET /???s/1/edit



35
36
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 35

def edit
end

#indexObject

GET /???s



21
22
23
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 21

def index
  @records = self.class.model.all
end

#newObject

GET /???s/new



30
31
32
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 30

def new
  @record = self.class.model.new
end

#showObject

GET /???s/1



26
27
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 26

def show
end

#updateObject

PATCH/PUT /???s/1



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/we_bridge/auto_view_helper/controller.rb', line 56

def update
  @record.class.transaction do
    p = record_params
    @record.update!(p) if p.present?
    record_text_params.try :each do |code,text_params|
      text = @record.text(code)
      text.attributes = text_params
      text.save!
    end
    flash[:notice]='Record was successfully updated.'
    redirect_to action: :show, id: @record.id
  end
rescue => e
  flash[:error]=e.message + e.backtrace.join("\n")
  redirect_to action: :edit, id: @record.id
end