brightcontent-core 2.1.5 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d35bf6ba253fbd2ddf7fe207a0fe4a1fbecfbf61
4
- data.tar.gz: ec5cfee1eb6b6f149d590f3db1a571b785d1d950
3
+ metadata.gz: ca3785050aa8c2db2a8007bb9a8a648b0f6cb13b
4
+ data.tar.gz: e8e2b8ffa1d5f558461b91b095b442bb6f9d860c
5
5
  SHA512:
6
- metadata.gz: 3949d392a27a5b32cbaad04de8a127a833293a4b5dd834cdba69fea8ee7e10e0ecab9ebf5471348f82dd77291aea6b5e8147e813c57c4833437a16905c4861e3
7
- data.tar.gz: 9fad89c2d9884156e629cac2f359e7570b09858901ba4e5df01a232aa3ecb05f7271fefd6c03e5bcfdcdabe9b4e4143babca296c3cb512f24b08b3050c7a87d7
6
+ metadata.gz: e29b0764abbb6d0032b59b0884a1555964db4fb686745febb4ffff0319c8b9afc1612f976dd939988f25f8ac2cbdb96b3ef7f74195135519cc3c2f9e4abd3614
7
+ data.tar.gz: 4537fa305e9fde2652e29e51ea08e0a4e2e2816d6c74e17fb5d5e9f6984cc1920952cdd7231589ddc8080eb59f5eaa61719b9989716c3aa508949ecebe98b4e9
data/Gemfile CHANGED
@@ -1,17 +1,3 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
- # Declare your gem's dependencies in brightcontent.gemspec.
4
- # Bundler will treat runtime dependencies like base dependencies, and
5
- # development dependencies will be added by default to the :development group.
6
3
  gemspec
7
-
8
- # jquery-rails is used by the dummy application
9
- # gem "jquery-rails"
10
-
11
- # Declare any dependencies that are still in development here instead of in
12
- # your gemspec. These might include edge Rails or gems from your path or
13
- # Git. Remember to move these dependencies to your gemspec before releasing
14
- # your gem to rubygems.org.
15
-
16
- # To use debugger
17
- # gem 'debugger'
@@ -1,3 +1,5 @@
1
+ require_dependency "brightcontent/application_controller"
2
+
1
3
  module Brightcontent
2
4
  class SessionsController < ApplicationController
3
5
  skip_before_filter :authorize
@@ -4,8 +4,8 @@ module Brightcontent
4
4
  ViewLookup::ListField.new(self, item: item, field: field).call
5
5
  end
6
6
 
7
- def render_filter_field(form, field)
8
- ViewLookup::FilterField.new(self, field: field, form: form).call
7
+ def render_filter_field(form, field, options)
8
+ ViewLookup::FilterField.new(self, field: field, form: form, options: options).call
9
9
  end
10
10
 
11
11
  def render_form_field(form, field)
@@ -17,5 +17,19 @@ module Brightcontent
17
17
  rescue ActionView::MissingTemplate
18
18
  nil
19
19
  end
20
+
21
+ # Returns filter field definitions as a nested array.
22
+ #
23
+ # [:foo, { bar: { as: :select }, qux: { as: :string } }]
24
+ #
25
+ # Becomes:
26
+ #
27
+ # [[:foo], [:bar, { as: :select }], [:qux, { as: :string }]]
28
+ #
29
+ def normalized_filter_fields
30
+ filter_fields.flat_map do |field|
31
+ field.is_a?(Hash) ? field.to_a : [[field]]
32
+ end
33
+ end
20
34
  end
21
35
  end
@@ -1,10 +1,8 @@
1
1
  <% if filter_fields.present? %>
2
2
  <div class="panel-body">
3
3
  <%= search_form_for ransack_search, class: "form-inline" do |form| %>
4
- <% filter_fields.each do |field| %>
5
- <div class="form-group">
6
- <%= render_filter_field form, field %>
7
- </div>
4
+ <% normalized_filter_fields.each do |field, options| %>
5
+ <%= render_filter_field form, field, options %>
8
6
  <% end %>
9
7
  <%= form.submit class: "btn btn-default btn-sm" %>
10
8
  <% end %>
@@ -15,18 +15,18 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- spec/*`.split("\n")
16
16
  s.require_paths = ["lib"]
17
17
 
18
- s.add_dependency "rails", ">= 4.0.0"
18
+ s.add_dependency "rails", ">= 4.2.0"
19
19
  s.add_dependency "bcrypt"
20
20
  s.add_dependency "bootstrap-sass", ">= 3.1"
21
21
  s.add_dependency "bootstrap-wysihtml5-rails", ">= 0.3.2"
22
+ s.add_dependency "inherited_resources", ">= 1.6.0"
22
23
  s.add_dependency "coffee-rails"
23
- s.add_dependency "inherited_resources", "~> 1.4.1"
24
24
  s.add_dependency "jquery-rails"
25
25
  s.add_dependency "sass-rails", ">= 4.0.2"
26
26
  s.add_dependency "simple_form"
27
27
  s.add_dependency "will_paginate"
28
28
  s.add_dependency "will_paginate-bootstrap"
29
- s.add_dependency "ransack", "~> 1.1"
29
+ s.add_dependency "ransack", ">= 1.1"
30
30
 
31
31
  s.add_development_dependency "rake"
32
32
  s.add_development_dependency "sqlite3"
@@ -1,3 +1,8 @@
1
+ # Make SimpleForm helpers available in Ransack's search form:
2
+ # https://github.com/activerecord-hackery/ransack#using-simpleform
3
+ #
4
+ ENV['RANSACK_FORM_BUILDER'] ||= '::SimpleForm::FormBuilder'
5
+
1
6
  require "coffee_script"
2
7
  require "bootstrap-sass"
3
8
  require "bootstrap-wysihtml5-rails"
@@ -7,7 +7,7 @@ module Brightcontent
7
7
 
8
8
  def initialize(view_context, options)
9
9
  @view_context = view_context
10
- @options = options
10
+ @options = options.symbolize_keys
11
11
  end
12
12
 
13
13
  def call
@@ -2,42 +2,110 @@ module Brightcontent
2
2
  module ViewLookup
3
3
  class FilterField < Base
4
4
  def render_default
5
- raise "invalid filter field: #{options[:field]}" unless field_name
6
- [
7
- options[:form].label(:"#{field_name}_eq", options[:field].humanize),
8
- options[:form].select(:"#{field_name}_eq", select_options, {include_blank: true}, class: "form-control input-sm")
9
- ].join(" ").html_safe
5
+ options[:form].input(input_name, input_options.merge(collection: collection))
10
6
  end
11
7
 
12
8
  private
13
9
 
14
- def field?
10
+ def controller
11
+ view_context.controller
12
+ end
13
+
14
+ def column?
15
15
  resource_class.column_names.include? options[:field].to_s
16
16
  end
17
17
 
18
+ def scope?
19
+ resource_class.ransackable_scopes.include?(options[:field].to_sym)
20
+ end
21
+
18
22
  def belongs_to_association?
19
23
  association.try :belongs_to?
20
24
  end
21
25
 
22
26
  def field_name
23
- if field?
24
- options[:field]
25
- elsif belongs_to_association?
27
+ if belongs_to_association?
26
28
  association.foreign_key
29
+ else
30
+ options[:field]
31
+ end
32
+ end
33
+
34
+ def input_name
35
+ if predicate
36
+ "#{field_name}_#{predicate}"
37
+ else
38
+ field_name.to_s
27
39
  end
28
40
  end
29
41
 
30
- def select_options
31
- if field?
32
- field_type == :boolean ? raw_options : raw_options.sort
42
+ def filter_options
43
+ options[:options] || {}
44
+ end
45
+
46
+ def predicate
47
+ filter_options.fetch(:predicate) { default_predicate }
48
+ end
49
+
50
+ def default_predicate
51
+ if scope?
52
+ false
53
+ elsif as_string?
54
+ "cont"
55
+ else
56
+ "eq"
57
+ end
58
+ end
59
+
60
+ def input_options
61
+ default_input_options.merge(filter_options.except(:predicate))
62
+ end
63
+
64
+ def collection
65
+ collection = filter_options[:collection]
66
+
67
+ if collection.respond_to?(:call)
68
+ collection.call
69
+ elsif collection.is_a?(Symbol) && controller.respond_to?(collection, true)
70
+ controller.send(collection)
71
+ elsif collection
72
+ collection
73
+ elsif as_collection?
74
+ default_collection
75
+ end
76
+ end
77
+
78
+ def as_collection?
79
+ %i(select radio_buttons check_boxes).include?(input_options[:as].to_sym)
80
+ end
81
+
82
+ def as_string?
83
+ %i(string search).include?(input_options[:as].to_sym)
84
+ end
85
+
86
+ def default_input_options
87
+ {
88
+ label: options[:field].to_s.humanize,
89
+ input_html: { class: "form-control input-sm" },
90
+ required: false,
91
+ as: default_type,
92
+ include_blank: true
93
+ }
94
+ end
95
+
96
+ def default_type
97
+ (column? || belongs_to_association?) ? :select : :string
98
+ end
99
+
100
+ def default_collection
101
+ if column?
102
+ field_type == :boolean ? raw_collection : raw_collection.sort
33
103
  elsif belongs_to_association?
34
- association.klass.where(association.association_primary_key => raw_options).map do |record|
35
- [record, record[association.association_primary_key]]
36
- end
104
+ association.klass.where(association.association_primary_key => raw_collection)
37
105
  end
38
106
  end
39
107
 
40
- def raw_options
108
+ def raw_collection
41
109
  resource_class.uniq.pluck(field_name)
42
110
  end
43
111
  end
@@ -1,3 +1,3 @@
1
1
  class Brightcontent::BlogsController < Brightcontent::BaseController
2
- filter_fields %w[featured author]
2
+ filter_fields :featured, :author, name: { as: :string }, name_or_comments_text: { label: "Find" }, exclude_inactive: { as: :boolean }
3
3
  end
@@ -1,7 +1,3 @@
1
1
  class Author < ActiveRecord::Base
2
2
  has_many :blogs
3
-
4
- def to_s
5
- name
6
- end
7
3
  end
@@ -1,5 +1,11 @@
1
1
  class Blog < ActiveRecord::Base
2
2
  belongs_to :author
3
3
  has_many :comments
4
+
4
5
  scope :featured, ->{ where(:featured => true) }
6
+ scope :exclude_inactive, ->{ where(active: true) }
7
+
8
+ def self.ransackable_scopes(auth = nil)
9
+ [:exclude_inactive]
10
+ end
5
11
  end
@@ -47,6 +47,7 @@ module Dummy
47
47
  config.assets.enabled = true
48
48
 
49
49
  # config.i18n.enforce_available_locales = true
50
+ config.active_record.raise_in_transactional_callbacks = true
50
51
 
51
52
  # Version of your assets, change this if you want to expire all your assets
52
53
  config.assets.version = '1.0'
@@ -9,7 +9,7 @@ Dummy::Application.configure do
9
9
  config.action_controller.perform_caching = true
10
10
 
11
11
  # Disable Rails's static asset server (Apache or nginx will already do this)
12
- config.serve_static_assets = false
12
+ config.serve_static_files = false
13
13
 
14
14
  # Compress JavaScripts and CSS
15
15
  config.assets.compress = true
@@ -10,7 +10,7 @@ Dummy::Application.configure do
10
10
  config.cache_classes = true
11
11
 
12
12
  # Configure static asset server for tests with Cache-Control for performance
13
- config.serve_static_assets = true
13
+ config.serve_static_files = true
14
14
  config.static_cache_control = "public, max-age=3600"
15
15
 
16
16
  # Show full error reports and disable caching
@@ -0,0 +1,5 @@
1
+ class AddActiveToBlogs < ActiveRecord::Migration
2
+ def change
3
+ add_column :blogs, :active, :boolean, default: true
4
+ end
5
+ end
data/spec/factories.rb CHANGED
@@ -29,4 +29,9 @@ FactoryGirl.define do
29
29
  end
30
30
  end
31
31
  end
32
+
33
+ factory :comment do
34
+ text "Comment text"
35
+ blog
36
+ end
32
37
  end
@@ -41,6 +41,31 @@ feature "Resources index" do
41
41
  page_should_have_n_rows 9
42
42
  end
43
43
 
44
+ scenario "Filter by name" do
45
+ given_2_blog_items_with_different_names
46
+ visit_blogs_page
47
+ fill_in "Name", with: "oo"
48
+ click_button "Search"
49
+ page_should_have_n_rows 1
50
+ end
51
+
52
+ scenario "Filter by name or comments text" do
53
+ given_3_blog_items_and_a_comment
54
+ visit_blogs_page
55
+ fill_in "Find", with: "oo"
56
+ click_button "Search"
57
+ page_should_have_n_rows 2
58
+ end
59
+
60
+ scenario "Filter by exclude_inactive scope" do
61
+ given_an_active_and_inactive_blog
62
+ visit_blogs_page
63
+ page_should_have_n_rows 2
64
+ check "Exclude inactive"
65
+ click_button "Search"
66
+ page_should_have_n_rows 1
67
+ end
68
+
44
69
  def visit_blogs_page
45
70
  click_link "Blogs"
46
71
  end
@@ -71,4 +96,20 @@ feature "Resources index" do
71
96
  def given_10_per_page
72
97
  Brightcontent::BlogsController.class_eval { per_page 10 }
73
98
  end
99
+
100
+ def given_2_blog_items_with_different_names
101
+ create :blog, name: "Foo"
102
+ create :blog, name: "Bar"
103
+ end
104
+
105
+ def given_3_blog_items_and_a_comment
106
+ create :blog, name: "Foo"
107
+ create :blog, name: "Bar"
108
+ create :comment, text: "Doodles"
109
+ end
110
+
111
+ def given_an_active_and_inactive_blog
112
+ create :blog, name: "Foo", active: false
113
+ create :blog, name: "Bar", active: true
114
+ end
74
115
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Brightcontent
4
4
  describe ModelExtensions do
5
- let(:columns) { ["id", "name", "body", "created_at", "updated_at", "featured", "author_id"] }
5
+ let(:columns) { ["id", "name", "body", "created_at", "updated_at", "featured", "author_id", "active"] }
6
6
  subject(:blog) { Blog }
7
7
  its(:brightcontent_columns) { should eq columns }
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brightcontent-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Developers at Brightin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-18 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bcrypt
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -67,33 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.3.2
69
69
  - !ruby/object:Gem::Dependency
70
- name: coffee-rails
70
+ name: inherited_resources
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 1.6.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 1.6.0
83
83
  - !ruby/object:Gem::Dependency
84
- name: inherited_resources
84
+ name: coffee-rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.4.1
89
+ version: '0'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: 1.4.1
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: jquery-rails
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -168,14 +168,14 @@ dependencies:
168
168
  name: ransack
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - "~>"
171
+ - - ">="
172
172
  - !ruby/object:Gem::Version
173
173
  version: '1.1'
174
174
  type: :runtime
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - "~>"
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '1.1'
181
181
  - !ruby/object:Gem::Dependency
@@ -396,6 +396,7 @@ files:
396
396
  - spec/dummy/db/migrate/20130720211920_create_comments.rb
397
397
  - spec/dummy/db/migrate/20140512085432_add_author_id_to_blogs.rb
398
398
  - spec/dummy/db/migrate/20140512090832_create_authors.rb
399
+ - spec/dummy/db/migrate/20150219130156_add_active_to_blogs.rb
399
400
  - spec/dummy/lib/assets/.gitkeep
400
401
  - spec/dummy/public/404.html
401
402
  - spec/dummy/public/422.html
@@ -438,7 +439,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
438
439
  version: '0'
439
440
  requirements: []
440
441
  rubyforge_project:
441
- rubygems_version: 2.2.2
442
+ rubygems_version: 2.4.5
442
443
  signing_key:
443
444
  specification_version: 4
444
445
  summary: Brightcontent core
@@ -484,6 +485,7 @@ test_files:
484
485
  - spec/dummy/db/migrate/20130720211920_create_comments.rb
485
486
  - spec/dummy/db/migrate/20140512085432_add_author_id_to_blogs.rb
486
487
  - spec/dummy/db/migrate/20140512090832_create_authors.rb
488
+ - spec/dummy/db/migrate/20150219130156_add_active_to_blogs.rb
487
489
  - spec/dummy/lib/assets/.gitkeep
488
490
  - spec/dummy/public/404.html
489
491
  - spec/dummy/public/422.html