admino 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95d1dd4a6bc98fcad84bb9d2269ac8104bdc4beb
4
+ data.tar.gz: c5a3bf57e8c8c22abb5f3f26a45e324c8204415a
5
+ SHA512:
6
+ metadata.gz: 222f144346d9e0c3666f7eb682c716599bd883376265d7ad10bb257805d39e8d0cdcade04fd7ac3cf73d2ca9f5bedb888ac4b534db6df5c0e03aff2c535f9ca5
7
+ data.tar.gz: 8a1117eb95c79108304c0d27e02578843f9a3bb985718202d1401d5b44cd8c44c263d83bfa5a1cc67dbb1a15d90cbc66a45e30921b1487d0619ebea5bb557bc2
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ binstubs/
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - "2.1.1"
7
+
8
+ script: "bundle exec rake spec"
9
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # v0.0.1
2
+
3
+ * First release
4
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'coveralls', require: false
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Stefano Verna
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Admino
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'admino'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install admino
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
data/admino.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'admino/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'admino'
8
+ spec.version = Admino::VERSION
9
+ spec.authors = ['Stefano Verna']
10
+ spec.email = ['s.verna@cantierecreativo.net']
11
+ spec.description = %q{Make administrative views creation less repetitive}
12
+ spec.summary = %q{Make administrative views creation less repetitive}
13
+ spec.homepage = 'https://github.com/cantierecreativo/admino'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'showcase', '~> 0.2.5'
22
+ spec.add_dependency 'activesupport'
23
+ spec.add_dependency 'activemodel'
24
+ spec.add_dependency 'coercible'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.3'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'i18n'
31
+ spec.add_development_dependency 'rspec-html-matchers'
32
+ spec.add_development_dependency 'actionpack'
33
+ end
34
+
data/lib/admino.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ require "admino/version"
4
+ require "admino/query"
5
+ require "admino/table"
6
+
7
+ module Admino
8
+ end
9
+
@@ -0,0 +1,12 @@
1
+ require 'admino/query/base'
2
+ require 'admino/query/base_presenter'
3
+ require 'admino/query/configuration'
4
+ require 'admino/query/field'
5
+ require 'admino/query/group'
6
+ require 'admino/query/group_presenter'
7
+
8
+ module Admino
9
+ module Query
10
+ end
11
+ end
12
+
@@ -0,0 +1,94 @@
1
+ require 'active_model/naming'
2
+ require 'active_model/translation'
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/hash'
5
+
6
+ require 'admino/query/dsl'
7
+
8
+ module Admino
9
+ module Query
10
+ class Base
11
+ extend ActiveModel::Naming
12
+ extend ActiveModel::Translation
13
+ extend Dsl
14
+
15
+ attr_reader :params
16
+ attr_reader :groups
17
+ attr_reader :fields
18
+
19
+ def self.i18n_scope
20
+ :query
21
+ end
22
+
23
+ def initialize(params = nil, config = nil)
24
+ @params = ActiveSupport::HashWithIndifferentAccess.new(params)
25
+ @config = config
26
+
27
+ init_groups
28
+ init_fields
29
+ end
30
+
31
+ def scope(starting_scope = nil)
32
+ starting_scope ||= if config.starting_scope_callable
33
+ config.starting_scope_callable.call(self)
34
+ else
35
+ raise ArgumentError, 'no starting scope provided'
36
+ end
37
+
38
+ scope_builder = starting_scope
39
+
40
+ (fields + groups).each do |field|
41
+ scope_builder = field.augment_scope(scope_builder)
42
+ end
43
+
44
+ if config.ending_scope_callable
45
+ scope_builder.instance_exec(self, &config.ending_scope_callable)
46
+ else
47
+ scope_builder
48
+ end
49
+ end
50
+
51
+ def persisted?
52
+ false
53
+ end
54
+
55
+ def config
56
+ @config || self.class.config
57
+ end
58
+
59
+ def groups
60
+ @groups.values
61
+ end
62
+
63
+ def group_by_name(name)
64
+ @groups[name]
65
+ end
66
+
67
+ def fields
68
+ @fields.values
69
+ end
70
+
71
+ def field_by_name(name)
72
+ @fields[name]
73
+ end
74
+
75
+ private
76
+
77
+ def init_groups
78
+ @groups = {}
79
+ i18n_key = self.class.model_name.i18n_key
80
+ config.groups.each do |config|
81
+ @groups[config.name] = Group.new(config, params, i18n_key)
82
+ end
83
+ end
84
+
85
+ def init_fields
86
+ @fields = {}
87
+ config.fields.each do |config|
88
+ @fields[config.name] = Field.new(config, params)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
@@ -0,0 +1,34 @@
1
+ require 'showcase'
2
+
3
+ module Admino
4
+ module Query
5
+ class BasePresenter < Showcase::Presenter
6
+ presents_collection :groups
7
+
8
+ def form(options = {}, &block)
9
+ h.form_for(
10
+ self,
11
+ options.reverse_merge(default_form_options),
12
+ &block
13
+ )
14
+ end
15
+
16
+ def simple_form(options = {}, &block)
17
+ h.simple_form_for(
18
+ self,
19
+ options.reverse_merge(default_form_options),
20
+ &block
21
+ )
22
+ end
23
+
24
+ def default_form_options
25
+ {
26
+ as: :query,
27
+ method: :get,
28
+ url: h.request.fullpath
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,54 @@
1
+ module Admino
2
+ module Query
3
+ class Configuration
4
+ class Field
5
+ attr_reader :name
6
+ attr_reader :coerce_to
7
+
8
+ def initialize(name, options = {})
9
+ options.symbolize_keys!
10
+ options.assert_valid_keys(:coerce)
11
+
12
+ @name = name.to_sym
13
+
14
+ if coerce_to = options[:coerce]
15
+ @coerce_to = coerce_to.to_sym
16
+ end
17
+ end
18
+ end
19
+
20
+ class Group
21
+ attr_reader :name
22
+ attr_reader :scopes
23
+
24
+ def initialize(name, scopes)
25
+ @name = name.to_sym
26
+ @scopes = scopes.map(&:to_sym)
27
+ end
28
+ end
29
+
30
+ attr_reader :fields
31
+ attr_reader :groups
32
+ attr_accessor :starting_scope_callable
33
+ attr_accessor :ending_scope_callable
34
+
35
+ def initialize
36
+ @fields = []
37
+ @groups = []
38
+ end
39
+
40
+ def add_field(name, options = {})
41
+ Field.new(name, options).tap do |field|
42
+ self.fields << field
43
+ end
44
+ end
45
+
46
+ def add_group(name, scopes)
47
+ Group.new(name, scopes).tap do |group|
48
+ self.groups << group
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,30 @@
1
+ module Admino
2
+ module Query
3
+ module Dsl
4
+ def config
5
+ @config ||= Admino::Query::Configuration.new
6
+ end
7
+
8
+ def field(name, options = {})
9
+ config.add_field(name, options)
10
+
11
+ define_method name do
12
+ field_by_name(name).value
13
+ end
14
+ end
15
+
16
+ def group(name, scopes)
17
+ config.add_group(name, scopes)
18
+ end
19
+
20
+ def starting_scope(&block)
21
+ config.starting_scope_callable = block
22
+ end
23
+
24
+ def ending_scope(&block)
25
+ config.ending_scope_callable = block
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,52 @@
1
+ require 'coercible'
2
+ require 'active_support/hash_with_indifferent_access'
3
+ require 'active_support/core_ext/hash'
4
+
5
+ module Admino
6
+ module Query
7
+ class Field
8
+ attr_reader :params
9
+ attr_reader :config
10
+
11
+ def initialize(config, params)
12
+ @config = config
13
+ @params = ActiveSupport::HashWithIndifferentAccess.new(params)
14
+ end
15
+
16
+ def augment_scope(scope)
17
+ if present?
18
+ scope.send(scope_name, value)
19
+ else
20
+ scope
21
+ end
22
+ end
23
+
24
+ def value
25
+ value = params.fetch(:query, {}).fetch(param_name, nil)
26
+ if config.coerce_to
27
+ begin
28
+ coercer = Coercible::Coercer.new
29
+ coercer[value.class].send(config.coerce_to, value)
30
+ rescue Coercible::UnsupportedCoercion
31
+ nil
32
+ end
33
+ else
34
+ value
35
+ end
36
+ end
37
+
38
+ def present?
39
+ value.present?
40
+ end
41
+
42
+ def param_name
43
+ config.name
44
+ end
45
+
46
+ def scope_name
47
+ config.name
48
+ end
49
+ end
50
+ end
51
+ end
52
+