generic_resources 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/app/controllers/generic_resources/resources_controller.rb +73 -0
- data/app/views/generic_resources/resources/all.html.example.erb +12 -0
- data/app/views/generic_resources/resources/edit.html.erb +5 -0
- data/app/views/generic_resources/resources/index.example.html.erb +21 -0
- data/app/views/generic_resources/resources/new.html.erb +5 -0
- data/config/locales/generic_resources.de.yml +19 -0
- data/config/routes.rb +16 -0
- data/lib/generic_resource.rb +19 -0
- data/lib/generic_resources.rb +15 -0
- data/lib/generic_resources/acts_as_generic_resource.rb +27 -0
- data/lib/generic_resources/engine.rb +6 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9611c1b215fd694b2a7590b46d7ac98e619efc0
|
4
|
+
data.tar.gz: 29cb3f63aa94fb41499c1a9c88e2fa7bd723adde
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5eef7a89dfbee9c254504e46c5773e0702d1795cc602873f7c5802b5294a4358b8ae0dc5c9bfa0ffe915cd5db3d4dbf393ad39d91fe96d58866e7cae0bb237de
|
7
|
+
data.tar.gz: 036c1bf49a92799d2a6fa3131f378b5408a6badbc869d9a35795843b0b7d3224ba7b77609c881428c97bea851e51f9bf9ff3ceeaef5d470d5f567e456dd0d387
|
@@ -0,0 +1,73 @@
|
|
1
|
+
class GenericResources::ResourcesController < GenericResources.configuration.parent_controller.to_s.constantize
|
2
|
+
|
3
|
+
before_filter :assign_resource, except: [:all]
|
4
|
+
|
5
|
+
def all
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
def index
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def new
|
14
|
+
@resource = @resource_class.new
|
15
|
+
render layout: nil if request.xhr?
|
16
|
+
end
|
17
|
+
|
18
|
+
def edit
|
19
|
+
render layout: nil if request.xhr?
|
20
|
+
end
|
21
|
+
|
22
|
+
def create
|
23
|
+
@resource = @resource_class.new(resource_attributes)
|
24
|
+
if @resource.save
|
25
|
+
flash[:notice] = I18n.t('generic_resources.controller.flash.notice.created', resource_name: @resource_class.model_name.human)
|
26
|
+
else
|
27
|
+
flash[:error] = I18n.t('generic_resources.controller.flash.error.not_created',
|
28
|
+
resource_name: @resource_class.model_name.human, errors: @resource.errors.messages)
|
29
|
+
end
|
30
|
+
|
31
|
+
redirect_to generic_resources.url_for(action: 'index', resource_name: params[:resource_name])
|
32
|
+
end
|
33
|
+
|
34
|
+
def update
|
35
|
+
if @resource.update_attributes(resource_attributes)
|
36
|
+
flash[:notice] = I18n.t('generic_resources.controller.flash.notice.updated', resource_name: @resource_class.model_name.human)
|
37
|
+
else
|
38
|
+
flash[:error] = I18n.t('generic_resources.controller.flash.error.not_updated',
|
39
|
+
resource_name: @resource_class.model_name.human, errors: @resource.errors.messages)
|
40
|
+
end
|
41
|
+
|
42
|
+
redirect_to generic_resources.url_for(action: 'index', resource_name: params[:resource_name])
|
43
|
+
end
|
44
|
+
|
45
|
+
def destroy
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def resource_attributes
|
51
|
+
params.require(params[:resource_name]).permit(*GenericResource.resources[params[:resource_name]][:permitted_attributes])
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def assign_resource
|
56
|
+
if params[:resource_name].nil?
|
57
|
+
flash[:error] = I18n.t('generic_resources.controller.flash.error.resource_type_missing')
|
58
|
+
redirect_to :back
|
59
|
+
elsif GenericResource.resources[params[:resource_name]].nil?
|
60
|
+
flash[:error] = I18n.t('generic_resources.controller.flash.error.resource_type_not_found', resource_name: params[:resource_name])
|
61
|
+
redirect_to :back
|
62
|
+
else
|
63
|
+
@resource_class = GenericResource.resources[params[:resource_name]][:class]
|
64
|
+
|
65
|
+
if params[:id]
|
66
|
+
@resource = @resource_class.find(params[:id])
|
67
|
+
elsif params[:action] != 'new' && params[:action] != 'create'
|
68
|
+
@resources = @resource_class.page(params[:page] || 1).per(GenericResources.configuration.per_page)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<table class="generic-resources-table">
|
2
|
+
<%= table_head [t('generic_resources.table_header.resource_name'),
|
3
|
+
t('generic_resources.table_header.resource_description'),
|
4
|
+
t('generic_resources.table_header.resource_count')] %>
|
5
|
+
<% GenericResource.resources.each do |name, data| %>
|
6
|
+
<tr>
|
7
|
+
<td><%= link_to data[:class].model_name.human, generic_resources.generic_resources_resources_path(resource_name: name) %></td>
|
8
|
+
<td><%= t("generic_resources.resource_descriptions.#{name}") %></td>
|
9
|
+
<td><%= data[:class].count %></td>
|
10
|
+
</tr>
|
11
|
+
<% end %>
|
12
|
+
</table>
|
@@ -0,0 +1,5 @@
|
|
1
|
+
<%= form_for @resource, url: generic_resources.generic_resources_resource_path, html: { method: :patch} do |f| %>
|
2
|
+
<%= render "generic_resources/resources/forms/#{params[:resource_name]}", f: f %>
|
3
|
+
<br />
|
4
|
+
<%= f.submit t("generic_resources.forms.save_resource", resource_name: @resource_class.model_name.human) %>
|
5
|
+
<% end %>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<%= link_to t("generic_resources.forms.create_resource", resource_name: @resource_class.model_name.human),
|
2
|
+
generic_resources.new_generic_resources_resource_path, {class: "button fi-plus"} %>
|
3
|
+
<table class="process-table">
|
4
|
+
<%= table_head GenericResource.resources[params[:resource_name]][:overview_attributes].map {|a|
|
5
|
+
GenericResource.resources[params[:resource_name]][:class].human_attribute_name(a)
|
6
|
+
} %>
|
7
|
+
<% @resources.each do |resource| %>
|
8
|
+
<tr>
|
9
|
+
<% GenericResource.resources[params[:resource_name]][:overview_attributes].each_with_index do |a,i| %>
|
10
|
+
<td>
|
11
|
+
<% if i == 0 %>
|
12
|
+
<%= link_to resource.send(a), generic_resources.edit_generic_resources_resource_path(resource_name: params[:resource_name], id: resource.id) %>
|
13
|
+
<% else %>
|
14
|
+
<%= resource.send(a) %>
|
15
|
+
<% end %>
|
16
|
+
</td>
|
17
|
+
<% end %>
|
18
|
+
</tr>
|
19
|
+
<% end %>
|
20
|
+
</table>
|
21
|
+
<%= paginate(@resources, scope: generic_resources) %>
|
@@ -0,0 +1,5 @@
|
|
1
|
+
<%= form_for @resource, url: generic_resources.generic_resources_resources_path, html: { method: :post} do |f| %>
|
2
|
+
<%= render "generic_resources/resources/forms/#{params[:resource_name]}", f: f %>
|
3
|
+
<br />
|
4
|
+
<%= f.submit t("generic_resources.forms.create_resource", resource_name: @resource_class.model_name.human) %>
|
5
|
+
<% end %>
|
@@ -0,0 +1,19 @@
|
|
1
|
+
de:
|
2
|
+
generic_resources:
|
3
|
+
table_header:
|
4
|
+
resource_name: Elementtyp
|
5
|
+
resource_count: Anzahl
|
6
|
+
resource_description: Beschreibung/Info
|
7
|
+
controller:
|
8
|
+
flash:
|
9
|
+
error:
|
10
|
+
resource_type_missing: "Kein Resourcentyp angegeben"
|
11
|
+
resource_type_not_found: "Der Resourcentyp '%{resource_name}' wurde nicht gefunden"
|
12
|
+
not_created: "%{resource_name} konnte nicht gespeichert werden: %{errors}"
|
13
|
+
not_updated: "%{resource_name} konnte nicht aktualisiert werden: %{errors}"
|
14
|
+
notice:
|
15
|
+
created: "%{resource_name} wurde erfolgreich angelegt"
|
16
|
+
updated: "%{resource_name} wurde erfolgreich aktualisiert"
|
17
|
+
forms:
|
18
|
+
save_resource: "%{resource_name} speichern"
|
19
|
+
create_resource: "%{resource_name} anlegen"
|
data/config/routes.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module GenericResource
|
2
|
+
|
3
|
+
mattr_accessor :resources
|
4
|
+
|
5
|
+
def self.register_resource!(klass, permitted_attributes: [], overview_attributes: [])
|
6
|
+
@@resources ||= {}
|
7
|
+
@@resources[klass.name.underscore.gsub("/","_")] = {
|
8
|
+
class: klass,
|
9
|
+
permitted_attributes: permitted_attributes,
|
10
|
+
overview_attributes: overview_attributes
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
class UnregisteredResourceError < StandardError
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "generic_resources/engine"
|
2
|
+
require "generic_resources/acts_as_generic_resource"
|
3
|
+
require "generic_resource"
|
4
|
+
require 'gem_config'
|
5
|
+
require 'kaminari'
|
6
|
+
|
7
|
+
module GenericResources
|
8
|
+
include GemConfig::Base
|
9
|
+
|
10
|
+
with_configuration do
|
11
|
+
has :controller_filters, classes: [String, Array]
|
12
|
+
has :parent_controller, default: 'ApplicationController'
|
13
|
+
has :per_page, default: 50
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module GenericResources
|
2
|
+
module ActsAsGenericResource
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def acts_as_generic_resource(permitted_attributes: [], overview_attributes: [])
|
12
|
+
if permitted_attributes.empty?
|
13
|
+
permitted_attributes = (self.try(:column_names) || []) - ['id', 'created_at', 'updated_at']
|
14
|
+
end
|
15
|
+
|
16
|
+
if overview_attributes.empty?
|
17
|
+
overview_attributes = permitted_attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
GenericResource.register_resource!(self, permitted_attributes: permitted_attributes, overview_attributes: overview_attributes)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Base.send :include, GenericResources::ActsAsGenericResource
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: generic_resources
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Eck
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: gem_config
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: kaminari
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Use just one Controller to CRUD all your resources instead of having
|
56
|
+
lots of of scaffold controllers
|
57
|
+
email: florian.eck@el-digital.de
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- app/controllers/generic_resources/resources_controller.rb
|
63
|
+
- app/views/generic_resources/resources/all.html.example.erb
|
64
|
+
- app/views/generic_resources/resources/edit.html.erb
|
65
|
+
- app/views/generic_resources/resources/index.example.html.erb
|
66
|
+
- app/views/generic_resources/resources/new.html.erb
|
67
|
+
- config/locales/generic_resources.de.yml
|
68
|
+
- config/routes.rb
|
69
|
+
- lib/generic_resource.rb
|
70
|
+
- lib/generic_resources.rb
|
71
|
+
- lib/generic_resources/acts_as_generic_resource.rb
|
72
|
+
- lib/generic_resources/engine.rb
|
73
|
+
homepage: https://github.com/florianeck/generic_resources
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Easy CRUD Interface for Rails
|
96
|
+
test_files: []
|
97
|
+
has_rdoc:
|