fix the content entry repository in order to pass integration tests [WIP]
did
committed Mar 05, 2015
commit 512e999604ba15b91d7e3463a67f0c1d187f7b39
Showing 16
changed files with
104 additions
and 725 deletions
locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb
+1
-1
| @@ | @@ -28,7 +28,7 @@ module Locomotive |
| end | |
| def setup_associations(attributes) | |
| - | content_type.association_fields.each do |field| |
| + | content_type.associations.each do |field| |
| case field.type | |
| when :belongs_to | |
| setup_belongs_to_association(field, attributes) | |
locomotive/steam/adapters/memory.rb b/lib/locomotive/steam/adapters/memory.rb
+4
-0
| @@ | @@ -18,6 +18,10 @@ module Locomotive::Steam |
| _query(mapper, scope, &block) | |
| end | |
| + | def find(mapper, scope, id) |
| + | _query(mapper, scope) { where(_id: id) }.first |
| + | end |
| + | |
| private | |
| def _query(mapper, scope, &block) | |
locomotive/steam/adapters/memory/dataset.rb b/lib/locomotive/steam/adapters/memory/dataset.rb
+4
-2
| @@ | @@ -24,8 +24,10 @@ module Locomotive::Steam |
| def insert(record) | |
| @primary_key.increment! do |id| | |
| - | record[identity] = id |
| - | records[id] = record |
| + | # if there is already an id, use it |
| + | _id = record[identity] || id |
| + | record[identity] = _id |
| + | records[_id] = record |
| end | |
| end | |
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb
+4
-0
| @@ | @@ -45,6 +45,10 @@ module Locomotive::Steam |
| @content_type || attributes[:content_type] | |
| end | |
| + | def content_type_id |
| + | @content_type.try(&:_id) || attributes[:content_type_id] |
| + | end |
| + | |
| def content_type_slug | |
| content_type.slug | |
| end | |
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb
+8
-2
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| include Locomotive::Steam::Models::Entity | |
| extend Forwardable | |
| - | def_delegators :fields, :localized_fields_names, :association_fields |
| + | def_delegators :fields, :localized_names, :associations |
| def initialize(attributes = {}) | |
| super({ | |
| @@ | @@ -32,7 +32,13 @@ module Locomotive::Steam |
| def order_by | |
| name = self[:order_by] == 'manually' ? '_position' : self[:order_by] | |
| - | [name, self.order_direction] |
| + | |
| + | # check if name is an id of field |
| + | if field = fields.find(name) |
| + | name = field.name |
| + | end |
| + | |
| + | { name.to_sym => self.order_direction } |
| end | |
| end | |
locomotive/steam/models/entity.rb b/lib/locomotive/steam/models/entity.rb
+4
-0
| @@ | @@ -19,6 +19,10 @@ module Locomotive::Steam |
| end | |
| end | |
| + | def _id |
| + | self[:_id] |
| + | end |
| + | |
| def []=(name, value) | |
| attributes[name.to_sym] = value | |
| end | |
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb
+3
-3
| @@ | @@ -93,13 +93,13 @@ module Locomotive |
| end | |
| def add_localized_fields_to_mapper(mapper) | |
| - | unless self.content_type.localized_fields_names.blank? |
| - | mapper.localized_attributes(*self.content_type.localized_fields_names) |
| + | unless self.content_type.localized_names.blank? |
| + | mapper.localized_attributes(*self.content_type.localized_names) |
| end | |
| end | |
| def add_associations_to_mapper(mapper) | |
| - | self.content_type.association_fields.each do |field| |
| + | self.content_type.associations.each do |field| |
| mapper.association(field.type, field.name, self.class, field.association_options, &method(:prepare_repository_for_association)) | |
| end | |
| end | |
locomotive/steam/repositories/filesystem/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/content_entry.rb
+0
-166
| @@ | @@ -1,166 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | |
| - | # class ContentEntry < Struct.new(:loader, :site, :current_locale, :content_type_repository) |
| - | |
| - | # include Concerns::Queryable |
| - | |
| - | # set_collection model: Filesystem::Models::ContentEntry, sanitizer: Filesystem::Sanitizers::ContentEntry |
| - | |
| - | # # Engine: ??? |
| - | # def all(type, conditions = {}) |
| - | # conditions = { _visible: true }.merge(conditions || {}) |
| - | |
| - | # # priority: |
| - | # # 1/ order_by passed in the conditions parameter |
| - | # # 2/ the default order (_position) defined in the content type |
| - | # order_by = conditions.delete(:order_by)|| conditions.delete('order_by') || type.order_by |
| - | |
| - | # query(type) do |
| - | # where(conditions).order_by(order_by) |
| - | # end.all |
| - | # end |
| - | |
| - | # # Engine: content_type.entries.build(attributes) |
| - | # def build(type, attributes = {}) |
| - | # collection_options[:model].new(attributes).tap do |entry| |
| - | # # set the reference to the content type |
| - | # entry.content_type = type |
| - | # end |
| - | # end |
| - | |
| - | # # Engine: entry.save |
| - | # def persist(entry) |
| - | # return nil if entry.nil? |
| - | |
| - | # collection = memoized_collection(entry.content_type) |
| - | |
| - | # # slugify entry |
| - | # sanitizer.set_slug(entry, collection) |
| - | |
| - | # collection << entry # immediate result |
| - | |
| - | # # make sure we write it back to the data source |
| - | # loader.write(entry.content_type, entry.attributes) |
| - | # end |
| - | |
| - | # # Engine: all(conditions).count > 0 |
| - | # def exists?(type, conditions = {}) |
| - | # query(type) { where(conditions) }.all.size > 0 |
| - | # end |
| - | |
| - | # # Engine: not necessary |
| - | # def by_slug(type, slug) |
| - | # query(type) { where(_slug: slug) }.first |
| - | # end |
| - | |
| - | # # Engine: entry.send(:name) :-) |
| - | # def value_for(name, entry, conditions = {}) |
| - | # value = entry.send(name) |
| - | |
| - | # if value.respond_to?(:association) |
| - | # association(value, conditions || {}) |
| - | # else |
| - | # value |
| - | # end |
| - | # end |
| - | |
| - | # # Engine: entry.next |
| - | # def next(entry) |
| - | # next_or_previous(entry, 'gt', 'lt') |
| - | # end |
| - | |
| - | # # Engine: entry.previous |
| - | # def previous(entry) |
| - | # next_or_previous(entry, 'lt', 'gt') |
| - | # end |
| - | |
| - | # # Engine: content_type.entries.klass.send(:group_by_select_option, name, content_type.order_by_definition) |
| - | # def group_by_select_option(type, name) |
| - | # return {} if type.nil? || name.nil? || type.fields_by_name[name].type != :select |
| - | |
| - | # _groups = all(type).group_by(&name) |
| - | |
| - | # groups = content_type_repository.select_options(type, name).map do |option| |
| - | # { name: option, entries: _groups.delete(option) || [] } |
| - | # end |
| - | |
| - | # unless _groups.blank? |
| - | # groups << (empty = { name: nil, entries: [] }) |
| - | # _groups.values.each { |list| empty[:entries] += list } |
| - | # end |
| - | |
| - | # groups |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def type_from(slug) |
| - | # content_type_repository.by_slug(slug) |
| - | # end |
| - | |
| - | # def localized_slug(entry) |
| - | # localized_attribute(entry, :_slug) |
| - | # end |
| - | |
| - | # def association(metadata, conditions = {}) |
| - | # case metadata.type |
| - | # when :belongs_to then belongs_to_association(metadata) |
| - | # when :has_many then has_many_association(metadata, conditions) |
| - | # when :many_to_many then many_to_many_association(metadata, conditions) |
| - | # end |
| - | # end |
| - | |
| - | # def belongs_to_association(metadata) |
| - | # type = type_from(metadata.target_class_slug) |
| - | # by_slug(type, metadata.target_slugs.first) |
| - | # end |
| - | |
| - | # def has_many_association(metadata, conditions) |
| - | # many_association(metadata, |
| - | # { metadata.target_field => localized_slug(metadata.source) }.merge(conditions)) |
| - | # end |
| - | |
| - | # def many_to_many_association(metadata, conditions) |
| - | # many_association(metadata, |
| - | # { '_slug.in' => metadata.target_slugs }.merge(conditions)) |
| - | # end |
| - | |
| - | # def many_association(metadata, conditions) |
| - | # type = type_from(metadata.target_class_slug) |
| - | |
| - | # if order_by = metadata.order_by |
| - | # conditions = { order_by: order_by }.merge(conditions) |
| - | # end |
| - | |
| - | # all(type, conditions) |
| - | # end |
| - | |
| - | # def memoized_collection(content_type) |
| - | # slug = content_type.slug |
| - | # @collections ||= {} |
| - | |
| - | # return @collections[slug] if @collections[slug] |
| - | |
| - | # @collections[slug] = collection(content_type) |
| - | # end |
| - | |
| - | # def next_or_previous(entry, asc_op, desc_op) |
| - | # return nil if entry.nil? |
| - | |
| - | # type = entry.content_type |
| - | # column, direction = type.order_by.split |
| - | # operator = direction == 'asc' ? asc_op : desc_op |
| - | # value = localized_attribute(entry, column) |
| - | |
| - | # query(type) { where("#{column}.#{operator}" => value) }.first |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/models/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/models/content_entry.rb
+0
-145
| @@ | @@ -1,145 +0,0 @@ |
| - | # require 'chronic' |
| - | |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class ContentEntry < Base |
| - | |
| - | # ASSOCIATION_NAMES = [:belongs_to, :has_many, :many_to_many].freeze |
| - | |
| - | # set_localized_attributes [:_id, :_slug, :seo_title, :meta_description, :meta_keywords] |
| - | |
| - | # attr_accessor :content_type |
| - | |
| - | # def initialize(attributes = {}) |
| - | # super({ |
| - | # _visible: true, |
| - | # _position: 0, |
| - | # created_at: Time.now, |
| - | # updated_at: Time.now |
| - | # }.merge(attributes)) |
| - | # end |
| - | |
| - | # def _slug; self[:_slug]; end |
| - | # alias :_id :_slug |
| - | # alias :_permalink :_slug |
| - | |
| - | # def method_missing(name, *args, &block) |
| - | # if is_dynamic_attribute?(name) |
| - | # cast_value(name) |
| - | # elsif attributes.include?(name) |
| - | # self[name] |
| - | # else |
| - | # super |
| - | # end |
| - | # end |
| - | |
| - | # def valid? |
| - | # errors.clear |
| - | # content_type.fields_by_name.each do |name, field| |
| - | # next unless field.required? |
| - | # errors.add_on_blank(name.to_sym) |
| - | # end |
| - | # errors.empty? |
| - | # end |
| - | |
| - | # def content_type |
| - | # @content_type || attributes[:content_type] |
| - | # end |
| - | |
| - | # def content_type_slug |
| - | # content_type.slug |
| - | # end |
| - | |
| - | # def _label |
| - | # self[content_type.label_field_name] |
| - | # end |
| - | |
| - | # def localized_attributes |
| - | # self.class.localized_attributes + content_type.localized_fields_names |
| - | # end |
| - | |
| - | # def to_liquid |
| - | # Locomotive::Steam::Liquid::Drops::ContentEntry.new(self) |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def is_dynamic_attribute?(name) |
| - | # content_type.fields_by_name.has_key?(name) |
| - | # end |
| - | |
| - | # def cast_value(name) |
| - | # field = content_type.fields_by_name[name] |
| - | |
| - | # begin |
| - | # _cast_value(field) |
| - | # rescue Exception => e |
| - | # Locomotive::Common::Logger.info "[#{content_type.slug}][#{_label}] Unable to cast the \"#{name}\" field, reason: #{e.message}".yellow |
| - | # nil |
| - | # end |
| - | # end |
| - | |
| - | # def _cast_value(field) |
| - | # if ASSOCIATION_NAMES.include?(field.type) |
| - | # AssociationMetadata.new(field.type, self, field, [*attributes[field.name]]) |
| - | # elsif private_methods.include?(:"_cast_#{field.type}") |
| - | # send(:"_cast_#{field.type}", field.name) |
| - | # else |
| - | # attributes[field.name] |
| - | # end |
| - | # end |
| - | |
| - | # def _cast_integer(name) |
| - | # _cast_convertor(name, &:to_i) |
| - | # end |
| - | |
| - | # def _cast_float(name) |
| - | # _cast_convertor(name, &:to_f) |
| - | # end |
| - | |
| - | # def _cast_file(name) |
| - | # _cast_convertor(name) do |value| |
| - | # value.present? ? { 'url' => value } : nil |
| - | # end |
| - | # end |
| - | |
| - | # def _cast_date(name) |
| - | # _cast_time(name, :to_date) |
| - | # end |
| - | |
| - | # def _cast_date_time(name) |
| - | # _cast_time(name, :to_date) |
| - | # end |
| - | |
| - | # def _cast_time(name, end_method) |
| - | # _cast_convertor(name) do |value| |
| - | # value.is_a?(String) ? Chronic.parse(value).send(end_method) : value |
| - | # end |
| - | # end |
| - | |
| - | # def _cast_convertor(name, &block) |
| - | # if (value = attributes[name]).is_a?(Hash) |
| - | # value.each { |l, _value| value[l] = yield(_value) } |
| - | # else |
| - | # yield(value) |
| - | # end |
| - | # end |
| - | |
| - | # class AssociationMetadata < Struct.new(:type, :source, :field, :target_slugs) |
| - | # def association; true; end |
| - | # def inverse_of; field.inverse_of; end |
| - | # def target_class_slug; field.class_name; end |
| - | # def order_by; field[:order_by]; end |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/sanitizers/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/content_entry.rb
+0
-66
| @@ | @@ -1,66 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Sanitizers |
| - | |
| - | # class ContentEntry < Struct.new(:default_locale, :locales) |
| - | |
| - | # def apply_to(collection) |
| - | # collection.each do |entry| |
| - | # set_content_type(entry) |
| - | # add_label(entry) |
| - | # set_slug(entry, collection) |
| - | # end |
| - | # end |
| - | |
| - | # def set_slug(entry, collection) |
| - | # if entry._label.is_a?(Hash) |
| - | # entry[:_slug] ||= {} |
| - | # entry._label.each do |locale, label| |
| - | # entry[:_slug][locale] ||= slugify(label, collection, locale) |
| - | # end |
| - | # else |
| - | # entry[:_slug] ||= slugify(entry._label, collection) |
| - | # end |
| - | # end |
| - | |
| - | # def slugify(label, collection, locale = nil) |
| - | # base, index = label.permalink(false), nil |
| - | # _slugify = -> (i) { [base, i].compact.join('-') } |
| - | |
| - | # while !is_slug_unique?(_slugify.call(index), collection, locale) |
| - | # index = index ? index + 1 : 1 |
| - | # end |
| - | |
| - | # _slugify.call(index) |
| - | # end |
| - | |
| - | # def is_slug_unique?(slug, collection, locale) |
| - | # Filesystem::MemoryAdapter::Query.new(collection, locale) do |
| - | # where(_slug: slug) |
| - | # end.first.nil? |
| - | # end |
| - | |
| - | # def set_content_type(entry) |
| - | # entry.content_type = entry.attributes.delete(:content_type) |
| - | # end |
| - | |
| - | # def add_label(entry) |
| - | # value = entry.attributes.delete(:_label) |
| - | # name = entry.content_type.label_field_name |
| - | |
| - | # if entry.attributes[name].is_a?(Hash) # localized? |
| - | # entry.attributes[name][default_locale] = value |
| - | # else |
| - | # entry.attributes[name] ||= value |
| - | # end |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/yaml_loaders/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/content_entry.rb
+0
-51
| @@ | @@ -1,51 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module YAMLLoaders |
| - | |
| - | # class ContentEntry < Struct.new(:root_path, :cache) |
| - | |
| - | # include YAMLLoaders::Concerns::Common |
| - | |
| - | # def list_of_attributes(content_type) |
| - | # cache.fetch("data/#{content_type.slug}") { load_list(content_type) } |
| - | # end |
| - | |
| - | # def write(content_type, attributes) |
| - | # list = cache.read("data/#{content_type.slug}") |
| - | |
| - | # list << attributes.merge(content_type: content_type) |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def load_list(content_type) |
| - | # [].tap do |list| |
| - | # each(content_type.slug) do |label, attributes, position| |
| - | # default = { content_type: content_type, _position: position, _label: label.to_s } |
| - | # list << default.merge(attributes) |
| - | # end |
| - | # end |
| - | # end |
| - | |
| - | # def each(slug, &block) |
| - | # position = 0 |
| - | # load(File.join(path, "#{slug}.yml")).each do |element| |
| - | # label, attributes = element.keys.first, element.values.first |
| - | # yield(label, attributes, position) |
| - | # position += 1 |
| - | # end |
| - | # end |
| - | |
| - | # def path |
| - | # File.join(root_path, 'data') |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
spec/integration/repositories/content_entry_repository_spec.rb
+53
-0
| @@ | @@ -0,0 +1,53 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| + | |
| + | describe Locomotive::Steam::ContentEntryRepository do |
| + | |
| + | shared_examples_for 'a repository' do |
| + | |
| + | let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } |
| + | let(:locale) { :en } |
| + | let(:repository) { described_class.new(adapter, site, locale) } |
| + | |
| + | let(:type_repository) { Locomotive::Steam::ContentTypeRepository.new(adapter, site, locale) } |
| + | let(:type) { type_repository.by_slug('bands') } |
| + | |
| + | describe '#all' do |
| + | subject { repository.with(type).all } |
| + | it { expect(subject.size).to eq 3 } |
| + | end |
| + | |
| + | # describe '#by_slug' do |
| + | # subject { repository.by_slug('bands') } |
| + | # it { expect(subject.description).to eq 'List of bands' } |
| + | # end |
| + | |
| + | end |
| + | |
| + | context 'MongoDB' do |
| + | |
| + | it_should_behave_like 'a repository' do |
| + | |
| + | let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') } |
| + | let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | context 'Filesystem' do |
| + | |
| + | it_should_behave_like 'a repository' do |
| + | |
| + | let(:site_id) { 1 } |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } |
| + | |
| + | after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb
+2
-2
| @@ | @@ -6,7 +6,7 @@ require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/yaml_l |
| describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do | |
| let(:site_path) { default_fixture_site_path } | |
| - | let(:content_type) { instance_double('Bands', slug: 'bands', association_fields: []) } |
| + | let(:content_type) { instance_double('Bands', _id: 42, slug: 'bands', associations: []) } |
| let(:scope) { instance_double('Scope', locale: :en, context: { content_type: content_type }) } | |
| let(:loader) { described_class.new(site_path) } | |
| @@ | @@ -23,7 +23,7 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::ContentEntry do |
| context 'a content type with a belongs_to field' do | |
| let(:field) { instance_double('Field', name: 'band', type: :belongs_to) } | |
| - | let(:content_type) { instance_double('Songs', slug: 'songs', association_fields: [field]) } |
| + | let(:content_type) { instance_double('Songs', slug: 'songs', associations: [field]) } |
| it 'adds a new attribute for the foreign key' do | |
| expect(subject.first[:band_id]).to eq 'pearl-jam' | |
spec/unit/entities/content_type_spec.rb
+12
-10
| @@ | @@ -3,7 +3,7 @@ require 'spec_helper' |
| describe Locomotive::Steam::ContentType do | |
| let(:fields) { [instance_double('Field1', label: 'Title', name: :title, localized: false), instance_double('Field2', label: 'Author', name: :author, localized: true)] } | |
| - | let(:repository) { instance_double('FieldRepository', all: fields, first: fields.first) } |
| + | let(:repository) { instance_double('FieldRepository', all: fields, first: fields.first, find: nil) } |
| let(:content_type) { described_class.new(name: 'Articles') } | |
| before { allow(content_type).to receive(:fields).and_return(repository) } | |
| @@ | @@ -29,17 +29,10 @@ describe Locomotive::Steam::ContentType do |
| end | |
| - | # describe '#localized_fields_names' do |
| - | |
| - | # subject { content_type.localized_fields_names } |
| - | # it { is_expected.to eq [:author] } |
| - | |
| - | # end |
| - | |
| describe '#order_by' do | |
| subject { content_type.order_by } | |
| - | it { is_expected.to eq ['_position', 'asc'] } |
| + | it { is_expected.to eq(_position: 'asc') } |
| context 'specifying manually' do | |
| @@ | @@ -47,7 +40,16 @@ describe Locomotive::Steam::ContentType do |
| content_type.attributes[:order_by] = 'manually' | |
| content_type.attributes[:order_direction] = 'desc' | |
| end | |
| - | it { is_expected.to eq ['_position', 'desc'] } |
| + | it { is_expected.to eq(_position: 'desc') } |
| + | |
| + | end |
| + | |
| + | context 'order_by references an id of a field' do |
| + | |
| + | let(:field) { instance_double('Field', name: 'title') } |
| + | let(:repository) { instance_double('FieldRepository', all: fields, first: fields.first, find: field) } |
| + | |
| + | it { is_expected.to eq(title: 'asc') } |
| end | |
spec/unit/repositories/content_entry_repository_spec.rb
+9
-9
| @@ | @@ -4,7 +4,7 @@ require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| describe Locomotive::Steam::ContentEntryRepository do | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| let(:entries) { [{ content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }] } | |
| let(:locale) { :en } | |
| let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: %i(en fr)) } | |
| @@ | @@ -89,7 +89,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#next' do | |
| - | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| let(:entries) do | |
| [ | |
| { content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, | |
| @@ | @@ -121,7 +121,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe '#previous' do | |
| - | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| + | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| let(:entries) do | |
| [ | |
| { content_type_id: 1, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, | |
| @@ | @@ -168,7 +168,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| category: instance_double('SelectField', name: :category, type: :select, select_options: { en: ['cooking', 'bread'], fr: ['cuisine', 'pain'] }) | |
| } | |
| end | |
| - | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title, :category], fields_by_name: fields) } |
| + | let(:type) { build_content_type('Articles', order_by: '_position asc', label_field_name: :title, localized_names: [:title, :category], fields_by_name: fields) } |
| let(:name) { :category } | |
| let(:entries) do | |
| @@ | @@ -193,7 +193,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'belongs_to' do | |
| let(:field) { instance_double('Field', name: :author, type: :belongs_to, association_options: { target_id: 2 }) } | |
| - | let(:type) { build_content_type('Articles', label_field_name: :title, association_fields: [field]) } |
| + | let(:type) { build_content_type('Articles', label_field_name: :title, associations: [field]) } |
| let(:entries) { [{ content_type_id: 1, title: 'Hello world', author_id: 'john-doe' }] } | |
| let(:other_type) { build_content_type('Authors', _id: 2, label_field_name: :name, fields_by_name: { name: instance_double('Field', name: :name, type: :string) }) } | |
| let(:other_entries) { [{ content_type_id: 2, _slug: 'john-doe', name: 'John Doe' }] } | |
| @@ | @@ -220,7 +220,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'has_many' do | |
| let(:field) { instance_double('Field', name: :articles, type: :has_many, association_options: { target_id: 2, inverse_of: :author, order_by: 'position_in_author' }) } | |
| - | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field]) } |
| + | let(:type) { build_content_type('Authors', label_field_name: :name, associations: [field]) } |
| let(:entries) { [{ content_type_id: 1, _id: 1, name: 'John Doe' }] } | |
| let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } | |
| let(:other_entries) { | |
| @@ | @@ -253,7 +253,7 @@ describe Locomotive::Steam::ContentEntryRepository do |
| describe 'many_to_many' do | |
| let(:field) { instance_double('Field', name: :articles, type: :many_to_many, association_options: { target_id: 2, inverse_of: :authors }) } | |
| - | let(:type) { build_content_type('Authors', label_field_name: :name, association_fields: [field]) } |
| + | let(:type) { build_content_type('Authors', label_field_name: :name, associations: [field]) } |
| let(:entries) { [{ content_type_id: 1, _id: 1, name: 'John Doe', article_ids: ['hello-world', 'lorem-ipsum'] }] } | |
| let(:other_type) { build_content_type('Articles', _id: 2, label_field_name: :title, fields_by_name: { name: instance_double('Field', name: :title, type: :string) }) } | |
| let(:other_entries) { | |
| @@ | @@ -289,8 +289,8 @@ describe Locomotive::Steam::ContentEntryRepository do |
| _id: 1, | |
| slug: name.to_s.downcase, | |
| order_by: nil, | |
| - | localized_fields_names: [], |
| - | association_fields: [], |
| + | localized_names: [], |
| + | associations: [], |
| fields_by_name: {} | |
| }.merge(attributes)) | |
| end | |
spec/unit/repositories/filesystem/content_entry_spec.rb
+0
-268
| @@ | @@ -1,268 +0,0 @@ |
| - | # require 'spec_helper' |
| - | |
| - | # describe Locomotive::Steam::Repositories::Filesystem::ContentEntry do |
| - | |
| - | # let(:type) { instance_double('Articles', slug: 'articles', order_by: nil, label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| - | # let(:entries) { [{ content_type: type, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }] } |
| - | # let(:loader) { instance_double('Loader', list_of_attributes: entries) } |
| - | # let(:site) { instance_double('Site', default_locale: :en, locales: [:en, :fr]) } |
| - | # let(:locale) { :en } |
| - | |
| - | # let(:content_type_repository) { instance_double('ContentTypeRepository') } |
| - | # let(:repository) { Locomotive::Steam::Repositories::Filesystem::ContentEntry.new(loader, site, locale, content_type_repository) } |
| - | |
| - | # describe '#collection' do |
| - | |
| - | # subject { repository.send(:collection, type) } |
| - | |
| - | # it { expect(subject.size).to eq 1 } |
| - | |
| - | # describe 'once after the sanitizer has been applied' do |
| - | |
| - | # subject { repository.send(:collection, type).first } |
| - | |
| - | # it { expect(subject.class).to eq Locomotive::Steam::Repositories::Filesystem::Models::ContentEntry } |
| - | # it { expect(subject.title).to eq({ en: 'Update #1', fr: 'Mise a jour #1' }) } |
| - | # it { expect(subject._slug).to eq({ en: 'update-number-1', fr: 'mise-a-jour-number-1' }) } |
| - | # it { expect(subject.content_type).to eq type } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#all' do |
| - | |
| - | # let(:conditions) { nil } |
| - | # subject { repository.all(type, conditions) } |
| - | |
| - | # it { expect(subject.size).to eq 1 } |
| - | |
| - | # end |
| - | |
| - | # describe '#build' do |
| - | |
| - | # let(:attributes) { { title: 'Hello world' } } |
| - | # subject { repository.build(type, attributes) } |
| - | |
| - | # it { expect(subject.title).to eq 'Hello world' } |
| - | |
| - | # end |
| - | |
| - | # describe '#persist' do |
| - | |
| - | # let(:entry) { instance_double('NewEntry', _visible: true, content_type: type, _label: 'Hello world', attributes: { title: 'Hello world' }) } |
| - | # subject { repository.persist(entry) } |
| - | |
| - | # before do |
| - | # expect(entry).to receive(:[]).with(:_slug).and_return(nil) |
| - | # expect(entry).to receive(:[]=).with(:_slug, 'hello-world') |
| - | # expect(loader).to receive(:write).with(type, { title: 'Hello world' }) |
| - | # end |
| - | |
| - | # it { expect { subject }.to change { repository.all(type).size }.by(1) } |
| - | |
| - | # end |
| - | |
| - | # describe '#exists?' do |
| - | |
| - | # let(:conditions) { {} } |
| - | # subject { repository.exists?(type, conditions) } |
| - | |
| - | # it { expect(subject).to eq true } |
| - | |
| - | # context 'more specific conditions' do |
| - | |
| - | # let(:conditions) { { '_slug' => 'update-number-1' } } |
| - | # it { expect(subject).to eq true } |
| - | |
| - | # end |
| - | |
| - | # context 'conditions which do match any entries' do |
| - | |
| - | # let(:conditions) { { '_slug' => 'foo' } } |
| - | # it { expect(subject).to eq false } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#by_slug' do |
| - | |
| - | # let(:slug) { nil } |
| - | # subject { repository.by_slug(type, slug) } |
| - | |
| - | # it { is_expected.to eq nil } |
| - | |
| - | # context 'existing slug' do |
| - | # let(:slug) { 'update-number-1' } |
| - | # it { expect(subject.title).to eq({ en: 'Update #1', fr: 'Mise a jour #1' }) } |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#value_for' do |
| - | |
| - | # let(:name) { :title } |
| - | # let(:entry) { instance_double('Article', title: 'Hello world') } |
| - | |
| - | # subject { repository.value_for(name, entry) } |
| - | |
| - | # it { is_expected.to eq 'Hello world' } |
| - | |
| - | # describe 'association do' do |
| - | |
| - | # let(:author_type) { instance_double('AuthorType') } |
| - | # let(:entry) { instance_double('Article', _slug: 'hello-world', author: association, authors: association) } |
| - | |
| - | # before do |
| - | # allow(content_type_repository).to receive(:by_slug).with(:authors).and_return(:author_type) |
| - | # end |
| - | |
| - | # context 'belongs_to association' do |
| - | |
| - | # let(:association) { instance_double('Association', type: :belongs_to, association: true, target_class_slug: :authors, target_slugs: ['john-doe'], order_by: nil) } |
| - | # let(:name) { :author } |
| - | |
| - | # before do |
| - | # expect(repository).to receive(:by_slug).with(:author_type, 'john-doe').and_return('John Doe') |
| - | # end |
| - | |
| - | # it { expect(subject).to eq 'John Doe' } |
| - | |
| - | # end |
| - | |
| - | # context 'has_many association' do |
| - | |
| - | # let(:association) { instance_double('Association', type: :has_many, association: true, target_class_slug: :authors, target_field: :article, order_by: 'created_at') } |
| - | # let(:name) { :authors } |
| - | |
| - | # before do |
| - | # allow(association).to receive(:source).and_return(entry) |
| - | # expect(repository).to receive(:all).with(:author_type, { article: 'hello-world', order_by: 'created_at' }).and_return(%w(jane john)) |
| - | # end |
| - | |
| - | # it { expect(subject).to eq %w(jane john) } |
| - | |
| - | # end |
| - | |
| - | # context 'many_to_many association' do |
| - | |
| - | # let(:association) { instance_double('Association', type: :many_to_many, association: true, target_class_slug: :authors, target_slugs: %w(jane john), order_by: nil) } |
| - | # let(:name) { :authors } |
| - | |
| - | # before do |
| - | # expect(repository).to receive(:all).with(:author_type, { '_slug.in' => %w(jane john) }).and_return(%w(jane john)) |
| - | # end |
| - | |
| - | # it { expect(subject).to eq %w(jane john) } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#next' do |
| - | |
| - | # let(:type) { instance_double('Articles', slug: 'articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| - | # let(:entries) do |
| - | # [ |
| - | # { content_type: type, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, |
| - | # { content_type: type, _position: 1, _label: 'Update #2', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' }, |
| - | # { content_type: type, _position: 2, _label: 'Update #3', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' } |
| - | # ] |
| - | # end |
| - | |
| - | # let(:entry) { nil } |
| - | # subject { repository.next(entry) } |
| - | |
| - | # it { is_expected.to eq nil } |
| - | |
| - | # context 'being last' do |
| - | |
| - | # let(:entry) { instance_double('Entry', content_type: type, _position: 2) } |
| - | # it { repository.send(:collection, type).inspect; is_expected.to eq nil } |
| - | |
| - | # end |
| - | |
| - | # context 'being middle' do |
| - | |
| - | # let(:entry) { instance_double('Entry', content_type: type, _position: 1) } |
| - | # it { expect(subject._position).to eq 2 } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#previous' do |
| - | |
| - | # let(:type) { instance_double('Articles', slug: 'articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title], fields_by_name: { title: instance_double('Field', name: :title, type: :string) }) } |
| - | # let(:entries) do |
| - | # [ |
| - | # { content_type: type, _position: 0, _label: 'Update #1', title: { fr: 'Mise a jour #1' }, text: { en: 'added some free stuff', fr: 'phrase FR' }, date: '2009/05/12', category: 'General' }, |
| - | # { content_type: type, _position: 1, _label: 'Update #2', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' }, |
| - | # { content_type: type, _position: 2, _label: 'Update #3', title: { fr: 'Mise a jour #2' }, text: { en: 'bla bla', fr: 'blabbla' }, date: '2009/05/12', category: 'General' } |
| - | # ] |
| - | # end |
| - | |
| - | # let(:entry) { nil } |
| - | # subject { repository.previous(entry) } |
| - | |
| - | # it { is_expected.to eq nil } |
| - | |
| - | # context 'being first' do |
| - | |
| - | # let(:entry) { instance_double('Entry', content_type: type, _position: 0) } |
| - | # it { repository.send(:collection, type).inspect; is_expected.to eq nil } |
| - | |
| - | # end |
| - | |
| - | # context 'being middle' do |
| - | |
| - | # let(:entry) { instance_double('Entry', content_type: type, _position: 1) } |
| - | # it { expect(subject._position).to eq 0 } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # describe '#group_by_select_option' do |
| - | |
| - | # let(:type) { nil } |
| - | # let(:name) { nil } |
| - | |
| - | # subject { repository.group_by_select_option(type, name) } |
| - | |
| - | # it { is_expected.to eq({}) } |
| - | |
| - | # context 'select field' do |
| - | |
| - | # let(:fields) do |
| - | # { |
| - | # title: instance_double('TitleField', name: :title, type: :string), |
| - | # category: instance_double('SelectField', name: :category, type: :select, select_options: { en: ['cooking', 'bread'], fr: ['cuisine', 'pain'] }) |
| - | # } |
| - | # end |
| - | # let(:type) { instance_double('Articles', slug: 'articles', order_by: '_position asc', label_field_name: :title, localized_fields_names: [:title, :category], fields_by_name: fields) } |
| - | # let(:name) { :category } |
| - | |
| - | # let(:entries) do |
| - | # [ |
| - | # { content_type: type, _position: 0, _label: 'Recipe #1', category: 'cooking' }, |
| - | # { content_type: type, _position: 1, _label: 'Recipe #2', category: 'bread' }, |
| - | # { content_type: type, _position: 2, _label: 'Recipe #3', category: 'bread' }, |
| - | # { content_type: type, _position: 3, _label: 'Recipe #4', category: 'unknown' } |
| - | # ] |
| - | # end |
| - | |
| - | # before { allow(content_type_repository).to receive(:select_options).and_return(%w(cooking wine bread)) } |
| - | |
| - | # it { expect(subject.size).to eq 4 } |
| - | # it { expect(subject.map { |h| h[:name] }).to eq ['cooking', 'wine', 'bread', nil] } |
| - | # it { expect(subject.map { |h| h[:entries].size }).to eq [1, 0, 2, 1] } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |