Module: Iord::Crud

Extended by:
ActiveSupport::Concern
Included in:
Controller
Defined in:
lib/iord/crud.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#createObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/iord/crud.rb', line 81

def create
  create_resource
  result = @resource.save
  respond_to do |format|
    if result
      flash[:notice] = t('iord.flash.create.notice', model: resource_name)
      format.html { redirect_to resource_url }
    else
      format.html { render action: 'new' }
    end
    formats(format, created: result)
  end
  return result
end

#create_collectionObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/iord/crud.rb', line 20

def create_collection
  collection = resource_class.all
  if default(:unscoped)
    collection = collection.unscoped
    collection = collection.where(deleted_at: nil) if defined?(Mongoid::Paranoia) && resource_class < Mongoid::Paranoia
  end
  collection = collection.unscoped if default(:unscoped)
  collection.includes(*Array(default(:includes))) if default(:includes)
  collection
end

#create_resourceObject



78
79
80
# File 'lib/iord/crud.rb', line 78

def create_resource
  @resource = resource_class.new resource_params
end

#destroyObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/iord/crud.rb', line 117

def destroy
  result = destroy_resource
  respond_to do |format|
    if result
      flash[:notice] = t('iord.flash.destroy.notice', model: resource_name)
      format.html { redirect_to collection_url }
    else
      flash[:alert] = t('iord.flash.destroy.alert', model: resource_name)
      format.html { redirect_to collection_url }
    end
    formats(format, destroyed: result)
  end
  return result
end

#destroy_resourceObject



114
115
116
# File 'lib/iord/crud.rb', line 114

def destroy_resource
  @resource.destroy!
end

#editObject



70
71
72
73
74
75
76
# File 'lib/iord/crud.rb', line 70

def edit
  edit_resource
  respond_to do |format|
    format.html { render }
    formats(format)
  end
end

#edit_resourceObject



68
69
# File 'lib/iord/crud.rb', line 68

def edit_resource
end

#indexObject



34
35
36
37
38
39
40
# File 'lib/iord/crud.rb', line 34

def index
  index_collection
  respond_to do |format|
    format.html { render }
    formats(format)
  end
end

#index_collectionObject



31
32
33
# File 'lib/iord/crud.rb', line 31

def index_collection
  @collection = create_collection
end

#newObject



60
61
62
63
64
65
66
# File 'lib/iord/crud.rb', line 60

def new
  new_resource
  respond_to do |format|
    format.html { render }
    formats(format)
  end
end

#new_resourceObject



52
53
54
55
56
57
58
59
# File 'lib/iord/crud.rb', line 52

def new_resource
  @resource = resource_class.new
  resource_class.relations.each do |name, rel|
    if rel.autosave and @resource.respond_to? "build_#{name}".to_sym
      @resource.public_send "build_#{name}".to_sym
    end
  end
end

#showObject



44
45
46
47
48
49
50
# File 'lib/iord/crud.rb', line 44

def show
  show_resource
  respond_to do |format|
    format.html { render }
    formats(format)
  end
end

#show_resourceObject



42
43
# File 'lib/iord/crud.rb', line 42

def show_resource
end

#updateObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/iord/crud.rb', line 99

def update
  update_resource
  result = @resource.save
  respond_to do |format|
    if result
      flash[:notice] = t('iord.flash.update.notice', model: resource_name)
      format.html { redirect_to resource_url }
    else
      format.html { render action: 'edit' }
    end
    formats(format, updated: result)
  end
  return result
end

#update_resourceObject



96
97
98
# File 'lib/iord/crud.rb', line 96

def update_resource
  @resource.update_attributes(resource_params)
end