cells-dashboard 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: 7b3e98c82548f3b5a9e5442d2d9a3c995ce47bb7
4
+ data.tar.gz: 4f9f74ad9ee27b3ae7c3df4b9086401904309f56
5
+ SHA512:
6
+ metadata.gz: 904f958c8c75541b0e1d7bce6c9d2ae5b674007726820f1fe675973ef7b037ac254155e2c5cf42185cb53e4ae56b44271d1360f3065aa843e694682d0b092506
7
+ data.tar.gz: 1be6696dc04e768c6f8a12f4f9a5336f6ead369c08648dd7aefbf6d2ffeff1bbb7ce5a0d0e975843e7fef1f9904b5f6c5955f969a1de3eca42d5bd5ef9c19ce7
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cells-dashboard.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ben Morrall
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,68 @@
1
+ # cells-dashboard
2
+
3
+ Adds a DSL to the [apotonick/cells](https://github.com/apotonick/cells) gem that allows for grouping of cells (as widgets) into multiple independent Dashboards.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cells'
11
+ gem 'cells-dashboard'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cells-dashboard
21
+
22
+ ## Usage
23
+
24
+ Define your dashboards in your initializer (`config/initializers/cells_dashboard.rb`), using blocks with matching arguments to control if a cell should be diplayed.
25
+
26
+ ```ruby
27
+ # Define a dashboard named `users`
28
+ Cells::Dashboard.create :users do
29
+ # Render the `Users::ChangePasswordCell` cell, if the users password has expired
30
+ add_widget 'users/change_password' do |user|
31
+ user.password_expired?
32
+ end
33
+
34
+ # Render the `Users::HappyBirthdayCell` if today is the user's birthday
35
+ add_widget 'users/happy_birthday' do |user|
36
+ user.birthday == Date.today
37
+ end
38
+
39
+ # Render the `Users::CompletedProfileCell` if none of the conditional widgets will be displayed
40
+ fallback 'users/completed_profile'
41
+ end
42
+ ```
43
+
44
+ Define your cells, using a `#display` method that accepts the same arguments as the blocks in your initializer.
45
+
46
+ ```ruby
47
+ # app/cells/users/change_password_cell.rb
48
+ class Users::ChangePasswordCell < Cell::Rails
49
+ def display(user)
50
+ @user = user
51
+ render
52
+ end
53
+ end
54
+ ```
55
+
56
+ Add a `#render_dashboard` call to your views, ensuring the arguments given match the name of your dashboard, the conditional blocks in your initializer, and the `#display` methods in your cells.
57
+
58
+ ```erb
59
+ <%- render_dashboard :users, current_user %>
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/[my-github-username]/cells-dashboard/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib' << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,10 @@
1
+ module Cells
2
+ module DashboardHelper
3
+ def render_dashboard(dashboard_name, *args)
4
+ widgets = Cells::Dashboard.dashboard_widgets(dashboard_name, *args)
5
+ widgets.each do |widget|
6
+ concat render_cell(widget.cell_name, :display, *args)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cells/dashboard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cells-dashboard"
8
+ spec.version = Cells::Dashboard::VERSION
9
+ spec.authors = ["Ben Morrall"]
10
+ spec.email = ["bemo56@hotmail.com"]
11
+ spec.summary = %q{A Simple DSL for rendering Cells as a Dashboard}
12
+ spec.description = %q{Adds a DSL to the apotonick/cells gem that allows for grouping of cells (as widgets) into multiple independent Dashboards}
13
+ spec.homepage = "https://github.com/bmorrall/cells-dashboard"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 'cells'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,35 @@
1
+ require "cells/dashboard/version"
2
+
3
+ module Cells
4
+ module Dashboard
5
+ require "cells/dashboard/context"
6
+ require "cells/dashboard/widget"
7
+
8
+ class << self
9
+ # Adds a newly defined dashboard
10
+ def create(dashboard_name, &block)
11
+ context = dashboard(dashboard_name)
12
+ context.instance_eval(&block)
13
+ context.freeze
14
+ end
15
+
16
+ # Returns all widgets to be rendered for a dashboard
17
+ def dashboard_widgets(dashboard_name, *args)
18
+ dashboard(dashboard_name).widgets_for_arguments(*args)
19
+ end
20
+
21
+ protected
22
+
23
+ def dashboard(dashboard_name)
24
+ @dashboards ||= {}
25
+ @dashboards[dashboard_name.to_sym] ||= Context.new
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ if defined?(ActionController)
32
+ require File.join(File.dirname(__FILE__), '..', '..', 'app', 'helpers', 'cells', 'dashboard_helper')
33
+ ActionController::Base.helper(Cells::DashboardHelper)
34
+ end
35
+
@@ -0,0 +1,38 @@
1
+ module Cells::Dashboard
2
+ class Context
3
+ # Adds a widget to a list of possible widgets
4
+ # @param [&block] should return true when displayed within a view context
5
+ def add_widget(cell_name, &block)
6
+ widgets << Widget.new(cell_name, &block)
7
+ end
8
+
9
+ # Adds a fallback widget in the event no widgets are displayed
10
+ def fallback(cell_name)
11
+ fallback_widgets << Widget.new(cell_name)
12
+ end
13
+
14
+ def freeze
15
+ fallback_widgets.freeze
16
+ widgets.freeze
17
+ super
18
+ end
19
+
20
+ # Only displays widgets that evaluate to true for `args`
21
+ def widgets_for_arguments(*args)
22
+ context_widgets = widgets.select { |widget| widget.display? *args }
23
+ context_widgets.any? ? context_widgets : fallback_widgets
24
+ end
25
+
26
+ protected
27
+
28
+ # Widgets that are conditionally tested for display
29
+ def widgets
30
+ @widgets ||= []
31
+ end
32
+
33
+ # Widgets that are returned when no widgets are to be displayed
34
+ def fallback_widgets
35
+ @fallback_widgets ||= []
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Cells
2
+ module Dashboard
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Cells::Dashboard
4
+ class Widget
5
+ attr_reader :cell_name
6
+
7
+ def self.name_for_cell(cell_name)
8
+ case cell_name
9
+ when Class
10
+ # Accept Cell classes
11
+ name_for_cell(cell_name.name)
12
+ when Array
13
+ # Accept arrays of values
14
+ cell_name
15
+ else
16
+ # Accept the name of cells (safely removing cell suffix)
17
+ cell_name.to_s.underscore.sub(/_cell\z/, '')
18
+ end
19
+ end
20
+
21
+ # Creates a new widget that:
22
+ # - renders the #display method of a cell found using `cell_name`
23
+ # - renders only if `block` evaluates to true
24
+ def initialize(cell_name, &block)
25
+ @cell_name = Widget.name_for_cell(cell_name).freeze
26
+ @block = block if block_given?
27
+ end
28
+
29
+ # Returns true if the cell should be displayed
30
+ def display?(*args)
31
+ @block.nil? || !!@block.call(*args)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ describe Cells::Dashboard::Context do
4
+
5
+ describe '#freeze' do
6
+ it 'should prevent the user from adding widgets' do
7
+ context = Cells::Dashboard::Context.new
8
+ context.freeze
9
+ begin
10
+ context.add_widget 'enabled_widget'
11
+ fail 'error should have been raised'
12
+ rescue RuntimeError
13
+ # Pass!
14
+ end
15
+ end
16
+ it 'should prevent the user from adding a fallback' do
17
+ context = Cells::Dashboard::Context.new
18
+ context.freeze
19
+ begin
20
+ context.fallback 'enabled_widget'
21
+ fail 'error should have been raised'
22
+ rescue RuntimeError
23
+ # Pass!
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#widgets_for_arguments' do
29
+ it 'should return all widgets where display returns to true' do
30
+ context = Cells::Dashboard::Context.new
31
+ context.add_widget 'enabled_widget' do
32
+ true
33
+ end
34
+ context.add_widget 'disabled_widget' do
35
+ false
36
+ end
37
+ result = context.widgets_for_arguments
38
+ result.size.must_equal 1
39
+ result[0].cell_name.must_equal 'enabled_widget'
40
+ end
41
+ it 'should pass all arguments to the display methods on each widget' do
42
+ received_value = nil
43
+ context = Cells::Dashboard::Context.new
44
+ context.add_widget 'enabled_widget' do |argument_value|
45
+ received_value = argument_value
46
+ end
47
+ context.widgets_for_arguments 'example_argument'
48
+ received_value.must_equal 'example_argument'
49
+ end
50
+ it 'should return the fallback value if no widgets are enabled' do
51
+ context = Cells::Dashboard::Context.new
52
+ context.add_widget 'enabled_widget' do |argument_value|
53
+ false
54
+ end
55
+ context.fallback 'fallback_widget'
56
+ result = context.widgets_for_arguments
57
+ result.size.must_equal 1
58
+ result[0].cell_name.must_equal 'fallback_widget'
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ describe Cells::Dashboard::Widget do
4
+ describe '.name_for_widget' do
5
+ it 'handles Class values' do
6
+ class Cells::Dashboard::ExampleCell; end
7
+ Cells::Dashboard::Widget.name_for_cell(Cells::Dashboard::ExampleCell).must_equal 'cells/dashboard/example'
8
+ end
9
+ it 'handles Array values' do
10
+ Cells::Dashboard::Widget.name_for_cell(['a', :b, 'C']).must_equal ['a', :b, 'C']
11
+ end
12
+ it 'handles String parameters' do
13
+ Cells::Dashboard::Widget.name_for_cell('my_widgets/example_widget_cell').must_equal 'my_widgets/example_widget'
14
+ end
15
+ end
16
+
17
+ describe '#display?' do
18
+ it 'returns true with no block value' do
19
+ widget = Cells::Dashboard::Widget.new('example_widget')
20
+ widget.display?.must_equal true
21
+ end
22
+ it 'returns the value from the block' do
23
+ return_value = [true, false].sample
24
+ widget = Cells::Dashboard::Widget.new('example_widget') do
25
+ return_value
26
+ end
27
+ widget.display?.must_equal return_value
28
+ end
29
+ it 'passes all arguments to the block value' do
30
+ received_value_a = received_value_b = nil
31
+ widget = Cells::Dashboard::Widget.new('example_widget') do |argument_value_a, argument_value_b|
32
+ received_value_a = argument_value_a
33
+ received_value_b = argument_value_b
34
+ end
35
+ widget.display? 'value a', 'value b'
36
+ received_value_a.must_equal 'value a'
37
+ received_value_b.must_equal 'value b'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ describe Cells::Dashboard do
4
+ after(:each) do
5
+ Cells::Dashboard.instance_variable_set '@dashboards', nil
6
+ end
7
+
8
+ describe '.create' do
9
+ it 'should provide a dsl based off a Context object' do
10
+ Cells::Dashboard.create :example do
11
+ add_widget 'example_widget'
12
+ fallback 'fallback_widget'
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '.dashboard_widgets' do
18
+ it 'should return displayed widgets with a matching dashboard name' do
19
+ Cells::Dashboard.create :matching_name do
20
+ add_widget('matching_widget') { true }
21
+ end
22
+ Cells::Dashboard.create :other_name do
23
+ add_widget('other_widget') { true }
24
+ end
25
+ result = Cells::Dashboard.dashboard_widgets(:matching_name)
26
+ result.size.must_equal 1
27
+ result[0].cell_name.must_equal 'matching_widget'
28
+ end
29
+ it 'should return all widgets that evaluate for true in a given context' do
30
+ Cells::Dashboard.create :example do
31
+ add_widget('enabled_widget') { true }
32
+ add_widget('disabled_widget') { false }
33
+ end
34
+ result = Cells::Dashboard.dashboard_widgets(:example)
35
+ result.size.must_equal 1
36
+ result[0].cell_name.must_equal 'enabled_widget'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'cells/dashboard'
3
+
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cells-dashboard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Morrall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cells
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Adds a DSL to the apotonick/cells gem that allows for grouping of cells
56
+ (as widgets) into multiple independent Dashboards
57
+ email:
58
+ - bemo56@hotmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - app/helpers/cells/dashboard_helper.rb
69
+ - cells-dashboard.gemspec
70
+ - lib/cells/dashboard.rb
71
+ - lib/cells/dashboard/context.rb
72
+ - lib/cells/dashboard/version.rb
73
+ - lib/cells/dashboard/widget.rb
74
+ - test/cells/dashboard/context_test.rb
75
+ - test/cells/dashboard/widget_test.rb
76
+ - test/cells/dashboard_test.rb
77
+ - test/test_helper.rb
78
+ homepage: https://github.com/bmorrall/cells-dashboard
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A Simple DSL for rendering Cells as a Dashboard
102
+ test_files:
103
+ - test/cells/dashboard/context_test.rb
104
+ - test/cells/dashboard/widget_test.rb
105
+ - test/cells/dashboard_test.rb
106
+ - test/test_helper.rb
107
+ has_rdoc: