Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builders association reflector implemented
- Loading branch information
Showing
5 changed files
with
298 additions
and
117 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
releaf-core/app/builders/releaf/builders/association_reflector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class Releaf::Builders::AssociationReflector | ||
delegate :macro, :name, :klass, to: :reflection | ||
|
||
attr_accessor :reflection, :fields, :sortable_column_name, :sortable_cache | ||
|
||
def initialize(reflection, fields, sortable_column_name) | ||
self.reflection = reflection | ||
self.fields = fields | ||
self.sortable_column_name = sortable_column_name.to_sym | ||
end | ||
|
||
def sortable? | ||
if @sortable.nil? | ||
@sortable = (expected_order_clause == actual_order_clause) | ||
end | ||
|
||
@sortable | ||
end | ||
|
||
def destroyable? | ||
if @destroyable.nil? | ||
@destroyable = reflection | ||
.active_record | ||
.nested_attributes_options | ||
.fetch(reflection.name, {}) | ||
.fetch(:allow_destroy, false) | ||
end | ||
|
||
@destroyable | ||
end | ||
|
||
def actual_order_clause | ||
relation = reflection.klass.all | ||
|
||
if reflection.scope | ||
relation = relation.instance_exec(reflection.active_record, &reflection.scope) | ||
end | ||
|
||
extract_order_clause(relation) | ||
end | ||
|
||
def expected_order_clause | ||
relation = reflection.klass.all.order(sortable_column_name) | ||
extract_order_clause(relation) | ||
end | ||
|
||
def extract_order_clause(relation) | ||
relation.order_values.map{|value| value_as_sql(value) }.join(", ") | ||
end | ||
|
||
def value_as_sql(value) | ||
if value.respond_to?(:to_sql) | ||
value.to_sql | ||
else | ||
value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
releaf-core/spec/builders/builders/association_reflector_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
require 'spec_helper' | ||
|
||
describe Releaf::Builders::AssociationReflector, type: :class do | ||
let(:reflection){ Book.reflect_on_association("chapters") } | ||
subject{ described_class.new(reflection, :b, "xx") } | ||
|
||
describe "#initialize" do | ||
it "assigns given reflection" do | ||
expect(subject.reflection).to eq(reflection) | ||
end | ||
|
||
it "assigns given fields" do | ||
expect(subject.fields).to eq(:b) | ||
end | ||
|
||
it "normalizes to symbol and assigns given sortable name" do | ||
expect(subject.sortable_column_name).to eq(:xx) | ||
end | ||
end | ||
|
||
describe "#destroyable?" do | ||
context "when reflection allow to destroy through nested attributes" do | ||
it "returns true" do | ||
subject.reflection = Book.reflect_on_association("book_sequels") | ||
expect(subject.destroyable?).to be true | ||
end | ||
end | ||
|
||
context "when reflection does not allow to destroy through nested attributes" do | ||
it "returns false" do | ||
expect(subject.destroyable?).to be false | ||
end | ||
end | ||
|
||
it "caches result" do | ||
expect(subject.reflection.active_record).to receive(:nested_attributes_options).and_call_original.once | ||
subject.destroyable? | ||
subject.destroyable? | ||
subject.destroyable? | ||
end | ||
end | ||
|
||
describe "#sortable?" do | ||
context "when expected and actual order clauses are same" do | ||
it "returns true" do | ||
allow(subject).to receive(:expected_order_clause).and_return(:a) | ||
allow(subject).to receive(:actual_order_clause).and_return(:a) | ||
expect(subject.sortable?).to be true | ||
end | ||
end | ||
|
||
context "when expected and actual order clauses differs" do | ||
it "returns false" do | ||
allow(subject).to receive(:expected_order_clause).and_return(:a) | ||
allow(subject).to receive(:actual_order_clause).and_return(:b) | ||
expect(subject.sortable?).to be false | ||
end | ||
end | ||
|
||
it "caches result" do | ||
expect(subject).to receive(:expected_order_clause).and_return(:x).once | ||
expect(subject).to receive(:actual_order_clause).and_return(:y).once | ||
subject.sortable? | ||
subject.sortable? | ||
subject.sortable? | ||
end | ||
end | ||
|
||
describe "#actual_order_clause" do | ||
context "when scope exists within reflection" do | ||
it "returns actual reflection klass order clause with evaluated scope" do | ||
relation = reflection.klass.all.order(:item_position) | ||
allow(subject).to receive(:extract_order_clause).with(relation).and_return(:y) | ||
expect(subject.actual_order_clause).to eq(:y) | ||
end | ||
end | ||
|
||
context "when no scope exists within reflection" do | ||
it "returns actual reflection klass order clause" do | ||
subject.reflection = Book.reflect_on_association("sequels") | ||
relation = subject.reflection.klass.all | ||
allow(subject).to receive(:extract_order_clause).with(relation).and_return(:y) | ||
expect(subject.actual_order_clause).to eq(:y) | ||
end | ||
end | ||
end | ||
|
||
describe "#expected_order_clause" do | ||
it "returns expected reflection klass order clause for sortable column name" do | ||
subject.sortable_column_name = :title | ||
relation = reflection.klass.all.order(:title) | ||
allow(subject).to receive(:extract_order_clause).with(relation).and_return(:y) | ||
expect(subject.expected_order_clause).to eq(:y) | ||
end | ||
end | ||
|
||
describe "#extract_order_clause" do | ||
it "returns order clauses normalized to string for given relation" do | ||
relation = Book.order(genre: :desc) | ||
expect(subject.extract_order_clause(relation)).to eq("`books`.`genre` DESC") | ||
end | ||
|
||
context "when relation has no order clauses" do | ||
it "returns empty string" do | ||
relation = Book.all | ||
expect(subject.extract_order_clause(relation)).to eq("") | ||
end | ||
end | ||
end | ||
|
||
describe "#value_as_sql" do | ||
context "when given value respond to sql" do | ||
it "return resulting sql" do | ||
expect(subject.value_as_sql(Book.all)).to eq("SELECT `books`.* FROM `books`") | ||
end | ||
end | ||
|
||
context "when given value does not respond to sql" do | ||
it "return given value" do | ||
expect(subject.value_as_sql(12)).to eq(12) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.