Module: Iord::Crud
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #create ⇒ Object
- #create_collection ⇒ Object
- #create_resource ⇒ Object
- #destroy ⇒ Object
- #destroy_resource ⇒ Object
- #edit ⇒ Object
- #edit_resource ⇒ Object
- #index ⇒ Object
- #index_collection ⇒ Object
- #new ⇒ Object
- #new_resource ⇒ Object
- #show ⇒ Object
- #show_resource ⇒ Object
- #update ⇒ Object
- #update_resource ⇒ Object
Instance Method Details
#create ⇒ Object
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_collection ⇒ Object
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_resource ⇒ Object
78 79 80 |
# File 'lib/iord/crud.rb', line 78 def create_resource @resource = resource_class.new resource_params end |
#destroy ⇒ Object
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_resource ⇒ Object
114 115 116 |
# File 'lib/iord/crud.rb', line 114 def destroy_resource @resource.destroy! end |
#edit ⇒ Object
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_resource ⇒ Object
68 69 |
# File 'lib/iord/crud.rb', line 68 def edit_resource end |
#index ⇒ Object
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_collection ⇒ Object
31 32 33 |
# File 'lib/iord/crud.rb', line 31 def index_collection @collection = create_collection end |
#new ⇒ Object
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_resource ⇒ Object
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 |
#show ⇒ Object
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_resource ⇒ Object
42 43 |
# File 'lib/iord/crud.rb', line 42 def show_resource end |
#update ⇒ Object
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_resource ⇒ Object
96 97 98 |
# File 'lib/iord/crud.rb', line 96 def update_resource @resource.update_attributes(resource_params) end |