Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit assigns size inside notes #201

Merged
merged 8 commits into from
Dec 4, 2023
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ gem "rake", "~> 13.0"
gem "rspec-rails", "~> 6.0"
gem "sprockets-rails", "~> 3.2"
gem "matrix", "~> 0.4.2"

gem "sqlite3", "~> 1.6"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.9-arm64-darwin)
sqlite3 (1.6.9-x86_64-linux)
thor (1.3.0)
timeout (0.4.1)
tzinfo (2.0.6)
Expand All @@ -209,6 +211,7 @@ DEPENDENCIES
rake (~> 13.0)
rspec-rails (~> 6.0)
sprockets-rails (~> 3.2)
sqlite3 (~> 1.6)

RUBY VERSION
ruby 3.2.2p53
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/templates/rails_footnotes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# Change font size :
# f.font_size = '11px'

# Change default limit :
# f.default_limit = 25

# Allow to open multiple notes :
# f.multiple_notes = true
end if defined?(Footnotes) && Footnotes.respond_to?(:setup)
3 changes: 3 additions & 0 deletions lib/rails-footnotes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class << self

delegate :font_size, :to => Filter
delegate :font_size=, :to => Filter

delegate :default_limit, :to => Filter
delegate :default_limit=, :to => Filter
end

def self.before(&block)
Expand Down
4 changes: 3 additions & 1 deletion lib/rails-footnotes/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Filter
@@klasses = []
@@lock_top_right = false
@@font_size = '11px'
@@default_limit = 25

# Default link prefix is textmate
@@prefix = 'txmt://open?url=file://%s&amp;line=%d&amp;column=%d'
Expand All @@ -20,7 +21,8 @@ class Filter
# :multiple_notes => Set to true if you want to open several notes at the same time
# :lock_top_right => Lock a btn to toggle notes to the top right of the browser
# :font_size => CSS font-size property
cattr_accessor :no_style, :notes, :prefix, :multiple_notes, :lock_top_right, :font_size
# :default_limit => Default limit for ActiveRecord:Relation in assigns note
cattr_accessor :no_style, :notes, :prefix, :multiple_notes, :lock_top_right, :font_size, :default_limit

class << self
include Footnotes::EachWithRescue
Expand Down
9 changes: 7 additions & 2 deletions lib/rails-footnotes/notes/assigns_note.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "active_record"

module Footnotes
module Notes
class AssignsNote < AbstractNote
Expand Down Expand Up @@ -40,8 +42,11 @@ def content
def to_table
table = assigns.inject([]) do |rr, var|
class_name = assigned_value(var).class.name
var_name = var.to_s
rr << ["<strong>#{var.to_s}</strong>" + "<br /><em>#{class_name}</em>", escape(assigned_value(var).inspect)]
var_value = assigned_value(var)
if var_value.is_a?(ActiveRecord::Relation) && var_value.limit_value.nil?
var_value = var_value.limit(Footnotes::Filter.default_limit)
end
rr << ["<strong>#{var.to_s}</strong>" + "<br /><em>#{class_name}</em>", escape(var_value.inspect)]
end

table.unshift(['Name', 'Value'])
Expand Down
36 changes: 27 additions & 9 deletions spec/notes/assigns_note_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require "spec_helper"
require 'action_controller'
require "action_controller"
require "rails-footnotes/notes/assigns_note"
require "support/active_record"

describe Footnotes::Notes::AssignsNote do
let(:note) do
@controller = double
allow(@controller).to receive(:instance_variables).and_return([:@action_has_layout, :@status])
@controller.instance_variable_set(:@action_has_layout, true)
@controller.instance_variable_set(:@status, 200)
Footnotes::Notes::AssignsNote.new(@controller)
let(:controller) do
double(instance_variables: [:@action_has_layout, :@status]).tap do |c|
c.instance_variable_set(:@action_has_layout, true)
c.instance_variable_set(:@status, 200)
end
end
subject {note}
subject(:note) { Footnotes::Notes::AssignsNote.new(controller) }

before(:each) {Footnotes::Notes::AssignsNote.ignored_assigns = []}

it {should be_valid}
it { should be_valid }

describe '#title' do
subject { super().title }
Expand Down Expand Up @@ -47,4 +47,22 @@
], {:summary=>"Debug information for Assigns (2)"})
note.content
end

describe "when it is an ActiveRecord::Relation" do
let(:controller) do
double(instance_variables: [:@widgets]).tap do |c|
c.instance_variable_set(:@widgets, Widget.all)
end
end

it "should still work" do
expect(note).to receive(:mount_table).with(
[
["Name", "Value"],
["<strong>@widgets</strong><br /><em>ActiveRecord::Relation</em>", "#&lt;ActiveRecord::Relation []&gt;"],
], {:summary=>"Debug information for Assigns (1)"})
expect(note).to be_valid
note.content
end
end
end
22 changes: 22 additions & 0 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "active_record"

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"

ActiveRecord::Migration.verbose = false

ActiveRecord::Migration.create_table :widgets do |t|
t.string :name
t.timestamps
end

class Widget < ActiveRecord::Base
end

RSpec.configure do |config|
config.around do |example|
ActiveRecord::Base.transaction do
example.run
raise ActiveRecord::Rollback
end
end
end